Swift Dictionary Class

Source: Internet
Author: User
Tags allkeys

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
  1. Import Foundation
  2. Let keystring: nsstring = "One, three four five" ①
  3. var keys: Nsarray = keystring.componentsseparatedbystring ("") ②
  4. Let valuestring: nsstring = "Alpha Bravo Charlie Delta echo" ③
  5. var values: Nsarray = valuestring.componentsseparatedbystring ("") ④
  6. Vardict: nsdictionary = nsdictionary (Objects:keys, forkeys:values) ⑤
  7. NSLog ("%@", Dict.description) ⑥
  8. varvalue:nsstring = Dict.objectforkey ("three") as Nsstring⑦
  9. NSLog ("three =%@", value)
  10. Varkys = Dict.allkeys⑧
  11. For Item:anyobject in Kys {⑨
  12. var key = item as NSString
  13. NSLog ("%@-%@", Key, Dict.objectforkey (key) as NSString)
  14. }



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
  1. 2014-07-06 20:19:07.274 playgroundstub_osx[4110:303] {
  2. five = echo;
  3. four = Delta;
  4. one = Alpha;
  5. three = Charlie;
  6. both = Bravo;
  7. }
  8. 2014-07-06 20:19:07.281 playgroundstub_osx[4110:303] three = Charlie
  9. 2014-07-06 20:19:07.296 playgroundstub_osx[4110:303] One-alpha
  10. 2014-07-06 20:19:07.300 playgroundstub_osx[4110:303] Five-echo
  11. 2014-07-06 20:19:07.305 playgroundstub_osx[4110:303] Three-charlie
  12. 2014-07-06 20:19:07.308 playgroundstub_osx[4110:303] Two-bravo
  13. 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
  1. Import Foundation
  2. var mutable: nsmutabledictionary = nsmutabledictionary () ①
  3. Add Objects
  4. Mutable.setobject ("Tom", Forkey: "[email protected]") ②
  5. Mutable.setobject ("Bob", Forkey: "[email protected]")
  6. NSLog ("%@", mutable.description)
  7. var keys = Mutable.allkeys
  8. For item:anyobject in keys {
  9. var key = item as NSString
  10. NSLog ("%@-%@", Key, Mutable.objectforkey (key) as NSString)
  11. }



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
    1. 2014-07-06 20:42:11.596 playgroundstub_osx[4332:303] {
    2. "[email protected]" = Bob;
    3. "[email protected]" = Tom;
    4. }
    5. 2014-07-06 20:42:11.605 playgroundstub_osx[4332:303] [email protected]-Bob
    6. 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
  1. Import Foundation①
  2. Let keystring: nsstring = "One of the three four five"
  3. Let keys: Nsarray = keystring.componentsseparatedbystring ("")
  4. Let valuestring: nsstring = "Alpha Bravo Charlie Delta echo"
  5. Let values: Nsarray = valuestring.componentsseparatedbystring ("")
  6. Let foundationdict: nsdictionary = nsdictionary (objects:values, Forkeys:keys) ②
  7. Let swiftdict: Dictionary = Foundationdict③
  8. println (swiftdict.description)
  9. Let Value:anyobject? = swiftdict["three"]④
  10. println ("three = \ (value)")
  11. For (key, value) in Swiftdict {⑤
  12. println ("\ (key)-\ (value)")
  13. }



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

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.