Swift Learning notes Arrays and dictionaries

Source: Internet
Author: User

Both arrays and dictionaries in Swift are implemented in the form of structs.

First of all, this is very wonderful, I do not know why this design, this is out of a kind of consideration.

Use a dictionary in C #:

 dictionary<int , string  > D IC = new  dictionary<int , string  > (); Dictionary  <int , string  >            Dic1 = DiC; Dic. ADD ( 1 ,  " 123   "            ); dic1[ 1 ]= " 456   "             1 ]); 

In C #, this code I can see with my eyes closed it's equal to 456, but in Swift:

var dic:dictionary<int,string> = [1:"123"= dicdic1[ 1 " 456 " println (dic[1]!)

The result of this output is 123, do you believe it? This wonderful dictionary is actually a value type.

Change Var to let:

Let dic:dictionary<int,string> = [1:"123"= dicdic1[ 1 " 456 " println (dic[1]!)

Compile the error, I CA, incredibly is the value type. I don't know why the dictionary is going to make a value type, which means that I pass a dictionary in 1 ways to actually copy the past.

So I would not be in the function to pass the dictionary when you have to consider the InOut keyword is as follows:

Func setdic (inout dic:dictionary<int,string>) {    dic[2"789" }var dic:dictionary<Int,String> = [1:"123"]setdic ( &DiC) println (dic[2]!)

I am also drunk, do not know why the dictionary to use the structure type to design, Scala's variable hashmap is also the use of reference type design

Val dic:scala.collection.mutable.hashmap[int,string] =+ = (3→ "3"= + = (4→ "4") println ( DIC)

Wondering what kind of consideration is swift, is it a performance consideration to design a dictionary as a value type?

Looking at the swift array, the swift array, in the new Xcode version has a big change, the original array I don't spit groove, when used

You have no way of distinguishing whether he is a reference type or a value type, which is now uniformly changed to a value type, when an array is assigned to another, it is a copy, it is in some cases a copy, and some cases are not copied:

var a= [== aprintln (a[0]) println (b[0]) println (c[0]) a[0 ] =println (a[0]) println (b[0]) println (c[0]) a.insert (777, atindex:0) println (a[0]) println (b[0]) println (c[0])

Now the array is changed, the value type, and modifying one subscript does not affect the other. The original use of constant equal judgment, now also changed to = = Number

if (b[1...2] = = a[2...3]) {    println ("true")}if(b = = c) {    println ("true"  )}

However, this design must be a designer's consideration, and the use of LLVM's compilation architecture and the LVM compiler architecture is different, these are not very clear

The optimization of the intermediate code is also unclear, perhaps using value types, higher performance, perhaps performance considerations.

In the book, the array copy is close to the performance of the C language.

Swift Learning notes Arrays and dictionaries

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.