IOS dictionary NSDictionary and NSMutableDictionary knowledge points, iosnsdictionary

Source: Internet
Author: User

IOS dictionary NSDictionary and NSMutableDictionary knowledge points, iosnsdictionary

 

The elements in the dictionary are stored in the form of key-value pairs. The keys and values of key-value pairs are all arbitrary objects, but keys often use strings. The address of the objects stored in the dictionary is not sequential, dictionary traversal is divided into: the traversal of keys and values, the difference between the dictionary and the array: the array pays attention to order, and the dictionary can help us quickly find data, data is our purpose, the key is our method. NSMutableDictionary is a subclass of NSDictionary, so it has all the functions of NSDictionary. It mainly involves some common operations. For other operations, see the relevant documentation, the following code also uses a third-party plug-in BlocksKit;

A: NSDictionary is a data set composed of key-value pairs.
B: Key: It can be any object (typically an NSString type object), and the key must be a single value.
C: Value: It can be any object and cannot be nil
D: common method: we can find the corresponding value based on the key.
E: The dictionary can be changeable or immutable. The dictionary can dynamically add or delete elements.
F: Unlike arrays, the dictionary is unordered.

1: Some Common NSDictionary operations

 

// Create a dictionary NSDictionary * dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @ "wujy", @ "name", @ 29, @ "age", nil]; NSInteger Count = [dictionary count]; NSLog (@ "the number of dictionaries is % ld", Count); // obtain all keys in the dictionary NSEnumerator * enumeratorkey = [dictionary keyEnumerator]; for (NSObject * obj in enumeratorkey) {NSLog (@ "key: % @", obj); NSLog (@ "value found through key: % @", [dictionary objectForKey: obj]);} // obtain all value values in the dictionary NSEnumerator * enumeratorvalue = [dictionary objectEnumerator]; for (NSObject * obj in enumeratorvalue) {NSLog (@ "value: % @", obj);} // you can easily create NSDictionary * newDictionary = @ {@ "name" after the value column of the key column ": @ "wujy", @ "age": @ 29}; NSArray * keyArray = [newDictionary allKeys]; for (NSObject * obj in keyArray) {NSLog (@ "key value: % @", obj);} NSArray * valueArray = [newDictionary allValues]; for (NSObject * obj in valueArray) {NSLog (@ "value: % @", obj);} // blocks dicnsdictionary * blockDictionary = [NSDictionary dictionaryWithObjects: [[NSArray alloc] initWithObjects: @ 3, @ 15, @ 29, nil] forKeys: [[NSArray alloc] initWithObjects: @ "IOS", @ "work", @ "age", nil]; // traverse [blockDictionary bk_each: ^ (id key, id obj) {NSLog (@ "the key for using blocks is: % @", key ); NSLog (@ "corresponding value: % @", obj) ;}]; // The filter is stopped only when the first one meets the condition.

NSDictionary * matchDictionary = [blockDictionary bk_match: ^ BOOL (id key, id obj ){
BOOL select = [obj intValue] <10? YES: NO; return select;}]; NSLog (@ "match matches: % @", matchDictionary); // filter NSDictionary * selectDictionary = [blockDictionary bk_select: ^ BOOL (id key, id obj) {BOOL select = [obj intValue]> 10? YES: NO; return select;}]; NSLog (@ "select matches % @", selectDictionary); // Reverse filter NSDictionary * rejectDictionary = [blockDictionary bk_reject: ^ BOOL (id key, id obj) {BOOL select = [obj intValue]> 10? YES: NO; return select;}]; NSLog (@ "reject matches % @", rejectDictionary ); // modify each item NSDictionary * mapDictinary = [blockDictionary bk_map: ^ id (id key, id obj) {return [NSNumber numberWithInt: [obj intValue] + 1];}]; NSLog (@ "the modified map item is % @", mapDictinary ); // if the condition is met, return bool BOOL isSelected = [blockDictionary bk_any: ^ BOOL (id key, id obj) {BOOL select = [obj intValue]> 20? YES: NO; return select;}]; NSLog (@ "% d meets the condition", isSelected ); // determine if all items meet this condition BOOL allSelected = [blockDictionary bk_all: ^ BOOL (id key, id obj) {BOOL select = [obj intValue]> 20? YES: NO; return select;}]; NSLog (@ "% d meets the condition", allSelected ); // determine if all items do not meet this condition BOOL noneSelected = [blockDictionary bk_none: ^ BOOL (id key, id obj) {BOOL select = [obj intValue]> 50? YES: NO; return select;}]; NSLog (@ "% d meets the condition", noneSelected );

 

2: Some Common NSMutableDictionary operations

// Create an optional nsmutabledictionary * mutableDictionary = [NSMutableDictionary dictionary failed: 3]; [mutableDictionary setObject: @ "wujy" forKey: @ "name"]; [mutableDictionary setObject: @ "cnblogs.com/wujy" forKey: @ "webName"]; [mutableDictionary setObject: @ 29 forKey: @ "age"]; // obtain all keys in the dictionary NSEnumerator * enumeratorkey = [mutableDictionary keyEnumerator]; for (NSObject * obj in enumeratorkey ){ NSLog (@ "key: % @", obj); NSLog (@ "the value of value found through key is: % @", [mutableDictionary objectForKey: obj]);} // delete one of the keys [mutableDictionary removeObjectForKey: @ "name"]; NSLog (@ "Delete the key after name: % @", mutableDictionary); [mutableDictionary removeAllObjects]; NSLog (@ "after deleting all, the number of variable dictionaries is % ld", [mutableDictionary count]); // blocks // filter NSMutableDictionary * newmutableDictionary = [NSMutableDictionary dictionaryWithDictionary: @ {@ "IOS": @ 3, @ "work": @ 13, @ "age": @ 29}]; [newmutableDictionary bk_performSelect: ^ BOOL (id key, id obj) {BOOL select = [obj intValue]> 10? YES: NO; return select;}]; NSLog (@ "the filtered variable dictionary is () % @", newmutableDictionary); // obtain the anti-filter [newmutableDictionary bk_1_mreject: ^ BOOL (id key, id obj) {BOOL select = [obj intValue]> 20? YES: NO; return select;}]; NSLog (@ "the variable dictionary for anti-filtering is (13) % @", newmutableDictionary ); // modify the results of each item [newmutableDictionary bk_performMap: ^ id (id key, id obj) {return @ ([obj intValue] + 1);}]; NSLog (@ "the modified variable dictionary is (14) % @", newmutableDictionary );

 

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.