swift-08-tuple decomposition and array

Source: Internet
Author: User



//1. Sometimes it is necessary to split the data in the tuple for example:



var stu = (" Fan Bingbing ", "the female ")


//1) will be Stu assigns three variables to the data in the

var (name,age,gender) = Stu


//2) only partial variables are required , can be used . syntax assignment, or use an underscore to replace the blank position, as follows

var stu1 = (" Zhao Wei ","Max"," male " )


var (Name1,age1,_) = STU1


println (name)


// with Let You can only assign a value once when booting. equals constant

var (name,age,gender) = Stu

//

println (name)



// will be name when assigning other data, the data in the tuple does not change

name = " Li Chen "




2. Arrays

// An array is a data structure that stores data of the same data type in a certain order, using a name and subscript for reference.


//c language. int array[10]; Swift, using let to write several elements of length is a few, can only be assigned one time. Variable length with var guide .



// once the value is assigned length, the contents of the array cannot be changed

Let array:[Int]


Array = [1,2,3,4,5]


ARRAY[2] = 23

Array = [1,2,3,4,5]


// traversal of an array


Print (" first array:")


for (var i:Int =0; i<=4; i++)

{

Print(array[i])

}


println ()




// elements, variable-length arrays

var array1:[Int] = [1,2,3, 4,5]


array1 [0] = 2

Print (" second array:")



for (var i:Int =0; i<=4; i++)

{

Print(array1[i])

}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

swift-08-tuple decomposition and array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.