Objective-c Nsdictionary & Nsmutabledictionary

Source: Internet
Author: User
Tags allkeys

Now every day to seize the study, so strive to learn two knowledge points every day, as soon as possible to learn OC, and then into the development phase! Come on.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

@autoreleasepool {

#pragma mark-nsdictionary

Nsdictionary

Create a Dictionary object using the Initwithobjectsandkeys: Method

//During object creation, a one-time deposit of multiple key-value pairs , values (value) before, keys, key values are separated by numbers, and end with nil

//dictionaries and arrays are not the same , the internal elements of the dictionary are unordered inside are made up of entries (key-value pairs ) , so there is no order .

Nsdictionary *obj1= [[Nsdictionary alloc] Initwithobjectsandkeys:@ "Jack"@ "name", @ " male",@ "Sex", [NSNumber numberwithint:+],@ "age", Nil ];

Get value by key value

NSLog(@ "======= gets value====== through key");

nsstring *sex = [obj1 valueforkey:@ "Sex"];

NSLog (@ "%@", sex);

Get all value

NSLog(@ "=======allvalue======");

Nsarray *allobjectvalue = [obj1 allvalues];

NSLog (@ "%@", Allobjectvalue);

Get all keys

NSLog(@ "=======allkeys=======");

Nsarray *allobjectkey = [obj1 allKeys];

NSLog (@ "%@", Allobjectkey);

Convenience Dictionary convenience can only facilitate key, and then in the key to find value

NSLog(@ "===== convenient =========");

For (nsstring *key in obj1) {

NSLog (@ "%@", [Obj1 Valueforkey:key]);

}

#pragma mark-nsmutabledictionary

Nsmutabledictionary

//Use the Convenience builder to create a mutable Dictionary object

NSLog(@ "=======1========");

Nsmutabledictionary *obj2 = [nsmutabledictionary dictionarywithobjectsandkeys:@ "Henry",@ " Name "@" male "@" sex "@" + "@" age", Nil];

NSLog (@ "%@", obj2);

There are two ways to add a dictionary:

1. usually it's SetValue forkey because it's normal to nil .

2. You can also use SetObject forkey , but if you give nil , you'll crash .

// add

[Obj2 SetValue:@ "Forkeypath" :@ "weight"];

NSLog (@ "%@", obj2);

[Obj2 SetObject:@ "Forkey" :@ "weight"];

NSLog (@ "%@", obj2);

// Modify

SetValue Forkey Method: If the dictionary has a corresponding key, then the value is modified back , if there is no corresponding key, the corresponding key value pair will be added, So SetValue forkey can be used to modify and add

[Obj2 SetValue:@ "Forkey" :@ "weight"];

NSLog (@ "%@", obj2);

// Delete

The deletion consists of two methods:

1.removeObjectForKey Example: delete @ "Sex"

[Obj2 removeobjectforkey:@ "Sex"];

NSLog (@ "%@", obj2);

2.REMOVEALLOBJECTSS Delete all

[Obj2 removeallobjects];

NSLog (@ " without %@", obj2);

//Extensions : Use dictionaries and arrays to represent countries , provinces , cities , Regions

        nsdictionary *OBJ3 = [ Span class= "S9" >nsdictionary dictionarywithobjectsandkeys:@ " China ",@" Country ",@" Heilongjiang ",@" Shengfen " ,@ "Harbin" ,@ "City" ,@ "Acheng" ,@ "Diqu" nil];

nsdictionary *obj4 = [nsdictionary dictionarywithobjectsandkeys: @ "America"@ "Country" , @ "NewYork", @ "NY", @ "Newzexi", @ "NZX", @ "abc",@ "Diqu", Nil];

Nsarray *array = [[nsarray alloc] initwithobjects:obj3,obj4, Nil];

For (nsdictionary *dic in array) {

NSLog (@ "%@", dic);

}

}

return 0;

}

Objective-c Nsdictionary & Nsmutabledictionary

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.