Swift starts learning _02 from scratch

Source: Internet
Author: User



Swift the next day



A. Dictionary



The dictionary in Swift does not store arbitrary objects like OC, and the dictionary in Swift needs to define the type of stored key values in advance, can specify the type directly, or let him infer it himself.



The Dictionary of Swift uses theDictionary<KeyType, ValueType>方式来definition.



var dic = ["FirstName": "Chris", "LastName": "Paul"]//Create dictionary directly



var dic1:dictionary<string,string> = ["FirstName": "Chris", "LastName": "Paul"]//Create a dictionary while specifying the type of the key value, which is not prompted when the type is specified, Hard to hit.



dic["Team" = "LAC"//This allows you to add a key-value pair for the dictionary, and the same can be changed directly by the key to the corresponding value



var x = dic.updatevalue ("LAL", Forkey: "Team")//This method changes value by key, the return value is the previous value changed, if the corresponding key does not exist, will return nil, but the dictionary will add the key value pair



dic["Team"] = nil//can be removed from the dictionary by putting the key to nil



var x = Dic.removevalueforkey ("team")//key-value pairs are removed according to key, the same key error will return nil, here can not be received by X



println (Dic.keys)//Get all the keys, the same as in OC



var Keysarray = Array (dic.keys)//Remove all keys and construct an array, personal sense, if you want to receive in the desired format, need to build an object like this



var Testarray = dictionary<int,string> ()//Create an empty dictionary, specifying the type of key and value



TESTARRAY[3] = "Chris"//Add a key-value pair to an empty dictionary, you need to press the specified data type, or you will get an error



The Var object in Swift is mutable, and the Let object is immutable, that is, if you want to create an immutable array or dictionary, it can be modified to let, and the modification to Var is mutable (personal understanding)



Two. For loop



①. Traditional for loops are also available in Swift



for (var i = 0; i < 5; i++) {



println (i)///First of all, the first method is no different from the for loop used before



}



A cycle in the ②.swift



For I in 1...5{



println (i)//for. In, where 1 ... 5 is 1 to 5 meaning



}



③.let Power = 10



var answer = 1



For _ in 1...power {



answer++//This cycle, _ is to ignore the worth of access, just a simple loop, in this case is simply 10 cycles



}



④. Iterating through an array



var array = ["One", "both"]



For STR in array{//traversal array, str can be arbitrary



println (str)



}



⑤. Traversing a dictionary



var dictionary = ["FirstName": "Chris", "LastName": "Paul"]



for (Key,value) in dictionary{



println ("\ (key), \ (value)")//Traversal Dictionary



}



⑥. Traversing strings



For character in "Hello" {



println (character)//an intuitive here does not indicate that the type of the object being traversed is inappropriate, but it is estimated that this is Swift's grammatical habit, hoping to get used to it later.



}



Conclusion, today's swift learning so much, feel a bit accustomed to this grammar, in fact, compared to OC or a lot of simple, continue to insist!






Swift starts learning _02 from scratch


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.