Swift Learning meta-group

Source: Internet
Author: User

Tuples are not in OC, in Swift is a new addition, the concept of OC array is still thinking that since you can store the same type of elements, the different types of elements have nothing to store it, the answer is sad, OC does not have the concept of tuples, but Swift added this thing, but also very interesting, The following is my personal understanding of the tuple, if there is wrong, please point out, common progress, thank you

A tuple (touples) is a collection of different data types that can be stored, denoted by (), a chestnut

Let Touples = ("Xiaoyu", +, "math", 98.5)
println (Touples)

If you see a hint, you know we can go through touples.0 or. 1 or. 2 or. The elements of the tuple are accessed by the type of swift to know the corresponding data type

We can also create tuples in the second way

Let (name,_,_,score) = Touples

The above _ in Swift is a wildcard character, remember to ignore the meaning that you do not need the corresponding value, no

println (name)
println (Score)

In fact, I personally prefer the third way, clear, code easier to read

Let Touple = (name: "Xiaoyu", Age:16,souce: "Math", scores:98.5)
println (Touple.name)

Said the above a big push, I still do not know what the tuple can do, Balabala, only know how to create, read and so on, then we this meta-group of small partners can do something ah, below a little chestnut

Override Func Viewdidload () {              var touple = (name: "Xiaoyu", Age:16,souce: "Math", scores:98.5)        println ( Touple.name)        Swapme (&touple.name, B: &touple.souce)        println (touple)           }    func swapme<t > (inout a:t,inout b:t) {Let        temp = a        a = b        b = Temp    }
Using the code above, we secretly swapped math and name, (note that this can only be exchanged for the shape of the same type, T is a generic expression), but we learned the tuple can write

    Func swapme<t> (inout a:t,inout b:t) {        (A, b) = (b,a)    }

Well, you can achieve the same effect, not the letter, and then an array to do the experiment

        var Temparr = [5,7]        Swapme (&temparr[0], B: &temparr[1])        println (Temparr)

Well, that's a really nice time to go.

There is the tuple really put anything can ah, don't believe you can try the following operation

        var num =        println (num)        println (num.0.0.0.0)
Tuple another useful place is error handling, you can put the error directly in the tuple back





Reference: The Book of the Meow God

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

Swift Learning meta-group

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.