Nsdictionary and Nsmutabledictionary

Source: Internet
Author: User

Nsdictionary

1: What is a dictionary?

Nsdictionary in OC: value is found based on key, and everything stored in the dictionary is a key-value pair

2: How do I create a dictionary?

Method One:

Nsdictionary *dict = [nsdictionary dictionarywithobject:@ "lnj" forkey:@ "  Name"]; // get value based on key NSString *name = [dict objectforkey:@ "name"]; NSLog (@ "name =%@", name);

Method Two:

/

/Note: Key and value are one by one corresponding nsdictionary*dict = [Nsdictionary dictionarywithobjects:@[@"LNJ",@" -",@"1.75"] Forkeys:@[@"name",@" Age",@"Height"]]; NSLog (@"%@ %@ %@", [Dict Objectforkey:@"name"], [dict objectforkey:@" Age"], [dict objectforkey:@"Height"]); method Three:"Shorthand:nsdictionary*dict =@{key:value}; Nsdictionary*dict = @{@"name":@"LNJ"}; NSLog (@"%@", dict[@"name"]); Nsdictionary*dict = @{@"name":@"LNJ",@" Age":@" -",@"Height":@"1.75"}; NSLog (@"%@ %@ %@", dict[@"name"], dict[@" Age"], dict[@"Height"]);

3: How do I traverse a dictionary?

Nsdictionary *dict = @{@"name":@"LNJ",@" Age":@" -",@"Height":@"1.75"};//gets the number of keys and value in the dictionary, which is called the key in the dictionary and value is calledNSLog (@"count =%lu", [dict Count]);

Method one: old-fashioned for loop notation

 for (int0; i < Dict.count; + +i)    {//  get all keys in the dictionary    nsarray *keys = [Dict AllKeys];     // remove key    from current position //         NSLog (@ "%@", Keys[i]);    NSString *key = keys[i];     *value = Dict[key];    NSLog (@ "key =%@, value =%@", key, value);}

Method Two: Enhance the For loop notation

// How to traverse the dictionary through Forin will assign all keys to the previous obj  for inch dict) {    //        NSLog (@ "%@", key);    NSString *value = Dict[key];    NSLog (@ "key =%@, value =%@", key, value);}

Method Three: An iterator to the OC dictionary to traverse

[Dict enumeratekeysandobjectsusingblock:^ (ID-ID obj, BOOL *stop) {    NSLog (@ " key =%@, value =%@ " , key, obj);}];

4: How to read and write a dictionary file?

1. Writing dictionary data to a file

Nsdictionary *dict = @{@ "name":@ "lnj"@ "age  ":@"@"height":@ " 1.75 " }; [Dict writetofile: @" /users/xiaomage/desktop/info.plist [ Atomically:yes];

2. Reading the dictionary data from the file

    • Note: Dictionaries and arrays are different, and the data stored in the dictionary is unordered
Nsdictionary *newdict = [nsdictionary dictionarywithcontentsoffile:@ "/users/xiaomage/desktop/ Info.plist"]; NSLog (@ "%@"*arr = @[@, @ +, @ @5 ]; [Arr writetofile: @" /users/xiaomage/desktop/abc.plist [ Atomically:yes];

9.NSMutableDictionary

How to increase and revise a variable dictionary

1: How do I create an empty mutable dictionary?

Nsmutabledictionary *DICTM = [Nsmutabledictionary dictionary];

2: How do I add a key value pair to a mutable dictionary?

[DICTM SetObject:@"LNJ"Forkey:@"name"];//Shorthand:dictm[@"name"] =@"LNJ";//all key-value pairs in the incoming dictionary are removed to add to the DICTM[DICTM setvaluesforkeyswithdictionary:@{@" Age":@" -",@"Height":@"1.75"}];

3: How do I get the value of a mutable dictionary based on the key?

NSLog (@ "name =%@", dictm[@ "name"]);

4: How do I delete a variable dictionary key value pair?

[DICTM Removeobjectforkey:@ "name"]; [DICTM removeobjectsforkeys:@[@"Age"@ "height"]];

5: How do I modify a key-value pair in a mutable dictionary?

    • Using the SetObject method to assign a value to a key of the same name, the new value overwrites the old value
[DICTM setobject:@ " forkey":@ "Age"]; // abbreviation:dictm[@"Age"@ "the";

6: What are the considerations for using a mutable dictionary?

1. Cannot use @{} to create a mutable dictionary

    Nsmutabledictionary *DICTM = @{@ "name":@ "lnj"}; // compile will be an error    [DICTM setobject:@ "Forkey":@ "Age"];

2.

    • If it is an immutable dictionary, then key cannot be the same
    • If there is a key with the same name in the immutable dictionary, then the value corresponding to the key will not be saved
    • If it is in a mutable array, the following will overwrite the previous
Nsdictionary *dict = @{@"name":@"LMJ",@"name":@"LNJ"}; NSLog (@"dict =%@", Dict); Nsmutabledictionary*DICTM = [Nsmutabledictionary dictionarywithobjects:@[@"LMJ",@"LNJ"] Forkeys:@[@"name",@"name"]]; NSLog (@"dict =%@", DICTM);

Nsdictionary and Nsmutabledictionary

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.