Provides a collection of dictionaries in the foundation framework that are composed of key-value pairs. The key collection cannot be duplicated, and the value collection has no special requirements. The elements in the keys and values collection can be any object, but cannot be nil. The Foundation Framework Dictionary class is also divided into nsdictionary immutable dictionaries and nsmutabledictionary mutable dictionaries.
First, Nsdictionary class
Nsdictionary has many methods and properties, and the following summarizes its common methods and properties.
Initwithdictionary: constructor that creates nsdictionary objects through Swift's dictionary.
Initwithobjects:forkeys: A constructor that creates a Nsdictionary object from a collection of keys and a collection of values.
The length of the Count dictionary collection.
Objectforkey: Gets the value object by key.
AllKeys: Returns all key collections.
Here's a sample code for the Nsdictionary array:
[HTML]View Plaincopy
- Import Foundation
- Let keystring: nsstring = "One, three four five" ①
- var keys: Nsarray = keystring.componentsseparatedbystring ("") ②
- Let valuestring: nsstring = "Alpha Bravo Charlie Delta echo" ③
- var values: Nsarray = valuestring.componentsseparatedbystring ("") ④
- Vardict: nsdictionary = nsdictionary (Objects:keys, forkeys:values) ⑤
- NSLog ("%@", Dict.description) ⑥
- varvalue:nsstring = Dict.objectforkey ("three") as Nsstring⑦
- NSLog ("three =%@", value)
- Varkys = Dict.allkeys⑧
- For Item:anyobject in Kys {⑨
- var key = item as NSString
- NSLog ("%@-%@", Key, Dict.objectforkey (key) as NSString)
- }
Line ① and line ③ of the above code are NSString strings, which consist of words and spaces. The ② line and the ④ line code use spaces to split the string, and the return type is the Nsarray array.
The ⑤ Line code instantiates the Nsdictionary object, and the objects parameter is the value array Values,forkeys parameter is. The ⑥ Line Code Description property is the content of the obtained dictionary. The ⑦ line code reads the value corresponding to the key by means of the Objectforkey method, and is converted to the NSString type.
Line ⑧ code Dict.allkeys is to get all the key collection Kys, the ⑨ line code is the traversal key collection Kys.
The output results are as follows:
[HTML]View Plaincopy
- 2014-07-06 20:19:07.274 playgroundstub_osx[4110:303] {
- five = echo;
- four = Delta;
- one = Alpha;
- three = Charlie;
- both = Bravo;
- }
- 2014-07-06 20:19:07.281 playgroundstub_osx[4110:303] three = Charlie
- 2014-07-06 20:19:07.296 playgroundstub_osx[4110:303] One-alpha
- 2014-07-06 20:19:07.300 playgroundstub_osx[4110:303] Five-echo
- 2014-07-06 20:19:07.305 playgroundstub_osx[4110:303] Three-charlie
- 2014-07-06 20:19:07.308 playgroundstub_osx[4110:303] Two-bravo
- 2014-07-06 20:19:07.313 playgroundstub_osx[4110:303] Four-delta
Second, Nsmutabledictionary class
Nsmutabledictionary is a subclass of Nsdictionary, it has many methods and properties, and the following summarizes its commonly used methods and properties.
?-setobject:forkey: By key and value.
-removeobjectforkey: Removes the value by key.
Here's a sample code for the Nsdictionary array:
[HTML]View Plaincopy
- Import Foundation
- var mutable: nsmutabledictionary = nsmutabledictionary () ①
- Add Objects
- Mutable.setobject ("Tom", Forkey: "[email protected]") ②
- Mutable.setobject ("Bob", Forkey: "[email protected]")
- NSLog ("%@", mutable.description)
- var keys = Mutable.allkeys
- For item:anyobject in keys {
- var key = item as NSString
- NSLog ("%@-%@", Key, Mutable.objectforkey (key) as NSString)
- }
The preceding code, line ①, is instantiated nsmutabledictionary, and the ② line of code is to add keys and values through the SetObject method.
The output results are as follows:
[HTML]View Plaincopy
- 2014-07-06 20:42:11.596 playgroundstub_osx[4332:303] {
- "[email protected]" = Bob;
- "[email protected]" = Tom;
- }
- 2014-07-06 20:42:11.605 playgroundstub_osx[4332:303] [email protected]-Bob
- 2014-07-06 20:42:11.608 playgroundstub_osx[4332:303] [email protected]–tom
Iii. the relationship between Nsdictionary and dictionary
The relationship between Nsdictionary and dictionary is like the relationship between Nsarray and array, and Swift can bridge them automatically at the bottom, and the result of a Nsdictionary object bridging is [nsobject: Anyobject] Dictionary (the value is NSObject type, the key is a dictionary dictionary of type Anyobject).
Let's look at an example that uses dictionary and nsdictionary:
[HTML]View Plaincopy
- Import Foundation①
- Let keystring: nsstring = "One of the three four five"
- Let keys: Nsarray = keystring.componentsseparatedbystring ("")
- Let valuestring: nsstring = "Alpha Bravo Charlie Delta echo"
- Let values: Nsarray = valuestring.componentsseparatedbystring ("")
- Let foundationdict: nsdictionary = nsdictionary (objects:values, Forkeys:keys) ②
- Let swiftdict: Dictionary = Foundationdict③
- println (swiftdict.description)
- Let Value:anyobject? = swiftdict["three"]④
- println ("three = \ (value)")
- For (key, value) in Swiftdict {⑤
- println ("\ (key)-\ (value)")
- }
Code line ① is the introduction of foundation. The ② code declares and initializes the Nsdictionary dictionary, and the ③ line of code is to assign the Nsdictionary dictionary to the Dictionary dictionary, a process that also casts a type, not just nsdictionary to dictionary conversions, And their internal elements have also been transformed.
The ④ line code is the value of the three key from the dictionary dictionary, and its type is an optional anyobject type, because it is possible that this value cannot be taken. The ⑤ line of code is to traverse the dictionary dictionary key and Value collection.
For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485
Welcome to Luxgen iOS Classroom public Platform
Swift Dictionary Class