Swift Learning meta-group

Source: Internet
Author: User

There is no tuple in OC. In Swift, the new addition, the OC array concept is still thinking that since the same type of elements can be stored, the different types of elements have nothing to store it, the answer is very sad, OC does not have the concept of a tuple. Only Swift added this thing, it is also very interesting, the following is my personal understanding of the tuple, assuming that there is not correct, 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 should know that we can go through touples.0 or. 1 or. 2 or. The elements of a tuple, which are judged by the type of swift to know the corresponding data type

We can also create tuples in a different way

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

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

println (name)
println (Score)

In fact I personally prefer the third way, clear, code is 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 the tuple can do what, Balabala. Just know how to create, read and so on. So what can we do with this meta-group of Little friends, and here's 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 that the tuple would be able to write

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

Well. can achieve the same effect, do not believe, and then an array to do the experiment

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

Well, that's a real thing to do.

There is the yuan group really put anything can ah. Do not believe can try the following actions

        var num =        println (num)        println (num.0.0.0.0)
Tuples also have a practical place for error handling, and can put errors directly in the tuple to return





References: The Book of the Meow God

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.