A preliminary knowledge of Objective-c dictionary

Source: Internet
Author: User

The dictionary stores key-value pairs, and values can be obtained by keys

 

1. Create an immutable dictionary

1 // 1 f (1)
2 NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys: @ "one", @ "1", @ "two", @ "2", @ "three", @ "3", nil];
3 NSLog (@ "% @", dic);
4
5 // 2 Quickly create a dictionary
6 NSDictionary * dic2 = @ {@ "1": @ "one", @ "2": @ "two", @ "3": @ "three"};
7 NSLog (@ "% @", dic2);
2. The number of elements in the dictionary

 1 NSUInteger count = [dic2 count];

2 NSLog (@ "% lu", count);

 

3. Arrays can be stored in the dictionary

1 NSArray * arr = @ [@ "one", @ "two", @ "three"];
2 NSDictionary * dic3 = @ {@ "1": @ "one", @ "array": arr, @ "2": @ "two"};
3 NSLog (@ "% @", dic3);
 

4. The value in the dictionary

1 // 5 Take the value from the dictionary
2 NSArray * arr2 = [dic3 objectForKey: @ "array"];
3 NSLog (@ "% @", arr2);
4 // Quick access
5 NSArray * arr3 = dic3 [@ "array"];
6 NSLog (@ "% @", arr3);
 

5. Variable dictionary

5.1 Creation of variable dictionary

1 // Create
2 NSMutableDictionary * muDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @ "one", @ "1", @ "two", @ "2", nil];
3
4 NSDictionary * dic = @ {@ "1": @ "one", @ "2": @ "two"};
5
6 NSMutableDictionary * muDic2 = [[NSMutableDictionary alloc] initWithDictionary: dic];
7
8 NSLog (@ "% @", muDic2);
 

5.2 Add elements to the dictionary

1 [muDic2 setObject: @ "three" forKey: @ "3"];
2 NSLog (@ "% @", muDic2);
5.3 Reset dictionary (set method)

1 [muDic2 setDictionary: @ {@ "a": @ "one", @ "b": @ "two"}];
2 NSLog (@ "% @", muDic2);
5.4 Delete element

Delete key-value pairs corresponding to key

 1 [muDic2 removeObjectForKey: @ "a"];

2 NSLog (@ "% @", muDic2);

Delete all

 1

2 [muDic2 removeAllObjects];
3 NSLog (@ "% @", muDic2);

5.5 Print the elements in the dictionary

1 NSArray * arr = [muDic2 allKeys];
2 // arr stores all keys in the dictionary
3 NSLog (@ "% @", arr);
4 // Print the values in the dictionary muDic by fast enumeration
5 for (NSString * str in arr) {
6 // NSLog (@ "% @", [muDic2 objectForKey: str]);
7 NSLog (@ "% @", muDic2 [str]);
8         }
 

Expand

-(BOOL) isKindOfClass: (Class) aClass;

Determine whether it is a certain type

example

[obj isKindOfClass: [NSString class]];

Determine if obj is of type NSString and return value is of type BOOL

 

Understanding the Objective-C dictionary

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.