New methods for arrays in swif compared to arrays in OC

Source: Internet
Author: User

1) Arrays in Swfit can only hold values of the same type, unlike OC and Java

2) Swift initialization method

var s0 = [1,2,3,4,5]

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

var s2: [String]

var s3 = [Int] ()

In Swift if the copy operation S3 = S1, S3 copies a copy of the S1 value, which is a deep copy. Two-part values do not affect each other.

the value of s3[0] has changed, but the value of S1 has not changed. The output is 1, 2, 3, 4, 5.

Note: In this case, var s = [], at this time is not an array, cannot make s [1] = 3 The specific reason is not clear now!


3) IsEmpty (OC not) determines whether the count of array is 0

The above S2 is now unable to invoke the method because it is just a declaration that there is no real memory mismatch.


4) + = (OC array not) adds one or more values at the end, and adds one when it is equivalent to append. Multiple hours is equivalent to adding an array between them.

However, adding a value when writing the code will cause an error.


5) S1 [StartIndex ... endIndex] value = [StartIndex EndIndex]


6) How the array is traversed

    1. For ... in this is very common.
    2. Use tuples to return subscripts with specific values for (index, value) in enumerate in (array)

for (Index, value) inch Enumerate (S1) {

println ("s1[\ (index)] =\ (value)")

}

   

7) New creation mode [type] (Count:repeatedvalue)

var s6 = [String] (count:3 repeatedvalue: "Hello,world" )






New methods for arrays in swif compared to arrays in OC

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.