Swift # Dictionary

Source: Internet
Author: User

//: Playground-noun:a Place where people can playImport Uikitvar str="Hello, playground."/*Dictionary: Stores a set of unordered data formats: oc:nsdictionary *dict = [nsdictionary dictionarywithobject:@ "Lnj" forkey:@ "name"]; NSLog (@ "%@", dict); Nsdictionary *dict = [nsdictionary dictionarywithobjectsandkeys:@ "name", @ "LNJ", @ "age", @30, nil]; NSLog (@ "%@", dict); Nsdictionary *dict = @{@ "name": @ "LNJ", @ "age": @30}; NSLog (@ "%@", dict); Swift:*///key must be hash (String, Int, Float, Double, Bool), value is not requiredvar dict0 = ["name":"LNJ"," Age": -]println (dict0) var dict1:dictionary= ["name":"LNJ"," Age": -]println (Dict1) var dict2:dictionary<String,Any> = ["name":"LNJ"," Age": -]println (Dict2) var dict3:[string:any]= ["name":"LNJ"," Age": -]println (DICT3) var dict4:[string:any]= Dictionary (dictionaryliteral: ("name","LNJ"), (" Age", -)) println (DICT4)//Empty Dictionary:var dict01:dictionary<string,any> =[:]var dict02= dictionary<string,any>() var dict03=[String:any] ()//immutable groups: var dict:dictionary<string,any> = [:]//variable array: let dict:dictionary<string,any> = [:]/*dictionary operation Oc:1. Get nsdictionary *dict = @{@ "name": @ "LNJ" @ "Age": @30}; NSLog (@ "%@", dict[@ "name"]); 2. Modify Nsmutabledictionary *dict = [Nsmutabledictionary dictionarywithobjectsandkeys:@] Name "@" LNJ "@" age ", @30, Nil];d ict[@" name "] = @" Iversion "; NSLog (@ "%@", dict[@ "name"]); Nsmutabledictionary *dict = [nsmutabledictionary dictionarywithobjectsandkeys:@ "name", @ "LNJ", @ "age", @30, nil]; [Dict setobject:@ "iversion" forkey:@ "name"]; NSLog (@ "%@", dict[@ "name"]); Swift:*///1. Getvar dict04 = ["name":"LNJ"," Age": -]println (dict04["name"]!)//2. Modificationsvar dict05 = ["name":"LNJ"," Age": -]dict05["name"] ="Iverson"println (dict05["name"]!) var dict06= ["name":"LNJ"," Age": -]dict06.updatevalue ("Iverson", Forkey:"name") println (dict06["name"]!) var dict08= ["name":"LNJ"," Age": -]//Updatevalue Returns an optional type that returns nil if there are no keys in the dictionary that need to be updated, and returns the original value if one existsifLet orignal = Dict08.updatevalue ("Iverson", Forkey:"name") {println (dict08["name"]!) println (orignal)}var dict07= ["name":"LNJ"," Age": -]//Updatevalue Returns an optional type that returns nil and adds a new key-value pair to the dictionary if there are no keys in the dictionary that need to be updatedifLet orignal = Dict07.updatevalue ("Iverson", Forkey:"ABC") {println (dict07["ABC"]!) println (orignal)}println (dict07)/*4. Add Oc:nsmutabledictionary *dict = [nsmutabledictionary dictionarywithobjectsandkeys:@ "name", @ "LNJ", @ "age", @30 , nil];d ict[@ "height"] = @175; NSLog (@ "%@", dict); Nsmutabledictionary *dict = [nsmutabledictionary dictionarywithobjectsandkeys:@ "name", @ "LNJ", @ "age", @30, nil]; [Dict setobject:@175 forkey:@ "height"]; NSLog (@ "%@", dict); Swift:*/var dict09= ["name":"LNJ"," Age": -]dict09["Height"] =175;p rintln (dict09)/*5. Delete oc:nsmutabledictionary *dict = [nsmutabledictionary dictionarywithobjectsandkeys:@ "Lnj", @ "name", @30, @ "age" , nil]; [Dict removeobjectforkey:@ "name"]; NSLog (@ "%@", dict); Nsmutabledictionary *dict = [nsmutabledictionary dictionarywithobjectsandkeys:@ "Lnj", @ "name", @30, @ "age", nil]; [Dict removeallobjects]; NSLog (@ "%@", dict); Swift:*/var dict_1= ["name":"LNJ"," Age": -]dict_1.removevalueforkey ("name") println (dict_1) var dict_2= ["name":"LNJ"," Age": -]//Removevalueforkey Returns an optional type that returns nil and does nothing if there is no key to delete in the dictionary, deletes the value corresponding to the key if it exists, and returns the deleted valueifLet orignal = Dict_2.removevalueforkey ("names") {println (dict_2) println (orignal)}println (dict_2) var dict_3= ["name":"LNJ"," Age": -]dict_3.removeall (keepcapacity:true) println ("====--Traversal Dictionary--====");/*Traverse dictionary Oc:nsmutabledictionary *dict = [nsmutabledictionary dictionarywithobjectsandkeys:@ "Lnj", @ "name", @30, @ "age" , nil]; [Dict enumeratekeysandobjectsusingblock:^ (ID key, id obj, BOOL *stop) {NSLog (@ "key =%@ value =%@", key, obj);}]; Nsmutabledictionary *dict = [nsmutabledictionary dictionarywithobjectsandkeys:@ "Lnj", @ "name", @30, @ "age", nil]; Nsarray *keys = [Dict allkeys];for (NSString *key in keys) {NSLog (@ "%@", key);} Nsmutabledictionary *dict = [nsmutabledictionary dictionarywithobjectsandkeys:@ "Lnj", @ "name", @30, @ "age", nil]; Nsarray *values = [Dict allvalues];for (NSString *value in values) {NSLog (@ "%@", value);} Swift:*/var dict_a= ["name":"LNJ"," Age": -] for(Key, value)inchEnumerate (dict_a) {println ("key = \ (key) value = \ (value)")}var Dict_b= ["name":"LNJ"," Age": -] forKeyinchDict_b.keys{println ("key = \ (key)")}var Dict_c= ["name":"LNJ"," Age": -] forValueinchDict_c.values{println ("value = \ (value)")}var Dict_d= ["name":"LNJ"," Age": -] for(Key, value)inchDict_d{println ("key = \ (key) value = \ (value)")}

|--> Copyright (c) Bing Ma.

|--> GitHub RUL: https://github.com/SpongeBob-GitHub

Swift # Dictionary

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.