Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc
The content of this article will be described
1.NSDictionary and Nsmutabledictionary Overview
2. Examples of commonly used attribute methods (not commonly used in this article will not be covered)
a nsdictionary/nsmutabledictionary overview
Nsdictionary provides a key-value way to store data. In general, any object can be a key as long as it follows the Nscopying protocol. Where key cannot be the same (judged by isequal). Neither key nor value can be nil, and if you want to express an empty value, use Nsnull. The value in Nsdictionary is immutable.
Nsmutabledictionary is a subclass of Nsdictionary, which is a mutable dictionary.
Examples of properties methods commonly used in two nsdictionary
2.1 Creating and initializing
Create and initialize
(instancetype)dictionaryWithContentsOfFile:(NSString *)path(instancetype)dictionary;(instancetype)dictionaryWithDictionary:(NSDictionary *)(instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys(instancetype)dictionaryWithObjectsAndKeys:(id)firstObject
Initialization
- (NSDictionary *) init;- (nsdictionary *) Initwithcontentsoffile: (nsstring *) path;-< Span class= "Hljs-params" > (nsdictionary *) Initwithdictionary: (nsdictionary *) ;- (nsdictionary *) initwithobjects: (nsarray *) objects Forkeys: ( Nsarray *) keys;- (nsdictionary *) initwithobjectsandkeys: firstobject;
Personally, I am more accustomed to the latter one. Of course, the quick way to create it don't forget
Symbol
@{}
Example:
NSDictionary * emptyDic = [NSDictionary dictionary]; NSDictionary * firstDic = @{@"key":@"value", @"first":@"1"}; NSDictionary * secondDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",nil];
2.2 Count
Returns the number of key-value pairs
NSDictionary * dic = @{@"key1":@"1", @"key2":@"2"}; NSLog(@"%d",dic.count);
2.3 Isequaltodictionary Compare two dictionary content is the same.
NSDictionary * dic1 = @{@"key1":@"1", @"key2":@"2"}; NSDictionary * dic2 = @{@"key2":@"2", @"key1":@"1"}; if ([dic1 isEqualToDictionary:dic2]) { NSLog(@"Equal contents"); }
2.4 Objectforkey: And Valueforkey get content by attributes
NSDictionary * dic1 = @{@"key1":@"1", @"key2":@"2"}; NSLog(@"%@",[dic1 objectForKey:@"key1"]); NSLog(@"%@",[dic1 valueForKey:@"key2"]);
2.5 AllKeys and Allvalues get all the Key/value
NSDictionary * dic1 = @{@"key1":@"1", @"key2":@"2"}; NSArray * keys = [dic1 allKeys]; NSArray * values = [dic1 allValues];
2.6 Enumeratekeysandobjectsusingblock a block way through
这里的stop决定了是否停止遍历。NSDictionary * dic1 = @{@"key1":@"1", @"key2":@"2"};[dic1 enumerateKeysAndObjectsUsingBlock:^(ididBOOL *stop) { NSLog(@"%@=>%@",[key description],[obj description]); }];
2.7 Sort
Keyssortedbyvalueusingselector:
Keyssortedbyvalueusingcomparator:
Keyssortedbyvaluewithoptions:usingcomparator:
Returns an array of keys, sorted in order of value.
nsdictionary* Numsdic = @{@ (2):@"Second", @(1):@"First", @(3):@"Thrid"};nsdictionary* Strdic = @{@"Id_1":@"First", @"Id_3":@"Thrid", @"Id_2":@"Second"};Nsarray* Numssortedkeys = [Numsdic keyssortedbyvalueusingselector:@selector(compare:)];Nsarray* Strsortedkyes = [Strdic keyssortedbyvalueusingcomparator:^nscomparisonresult (IDObj1,IDOBJ2) {NSString* str1 = obj1;NSString* str2 = OBJ2;return[Str1 COMPARE:STR2]; }];NSLog(@"%@", Numssortedkeys. Description);NSLog(@"%@", Strsortedkyes. Description);
Output
2015-02-09 22:04:12.070 dictonaryexample[1037:23292] (
1,
2,
3
)
2015-02-09 22:04:12.070 dictonaryexample[1037:23292] (
"Id_1",
"Id_2",
"Id_3"
)
2.8 Filtration
Keysofentriespassingtest:
Returns a collection of keys that conform to the constraints of the parameter block
nsdictionary* Numsdic = @{@ (2):@"Second", @(1):@"First", @(3):@"Thrid"}; Nsset * Filteredkyes = [Numsdic keysofentriespassingtest:^BOOL(IDKeyIDObjBOOL*stop) {BOOLresult =NO;NSNumber* Numkey = key;if(Numkey. IntegerValue>2) {result =YES; }returnResult }];NSLog(@"%@", Filteredkyes. Description);
Output
2015-02-09 22:09:50.800 dictonaryexample[1099:25241] {(
3
)}
2.9 Write to file
Writetofile:atomically
Writetourl:atomically
NSStringYES) firstObject] ; NSString * fileName = @"file"; NSString * filePath = [path stringByAppendingPathComponent:fileName]; NSDictionary * dic = @{@"key":@"value"}; BOOL result = [dic writeToFile:filePath atomically:YES]; if (result) { NSLog(@"Success"); }
2.10description-is often used to debug output dictionary content.
I've had a lot of examples before, and I don't repeat them here.
three additional ways to Nsmutabledictionary
3.1 Adding elements
- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey- (void)setValue:(id)value forKey:(NSString *)key- (void)setDictionary:(NSDictionary *)otherDictionary
Note that when using KVC, key must be nsstring. The third function is to delete the previous element and then place the otherdictionary element in the current DIC.
Example
NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"object",@"key"nil]; NSLog(@"%@",dic.description); [dic setDictionary:@{@"otherKey":@"otherValue"}]; NSLog(@"%@",dic.description);
Output
2015-02-09 22:31:21.417 dictonaryexample[1232:31666] {
Key = object;
}
2015-02-09 22:31:21.418 dictonaryexample[1232:31666] {
OtherKey = Othervalue;
}
3.2 Deleting an element
- (void)removeObjectForKey:(id)aKey- (void)removeAllObjects- (void)removeObjectsForKeys:(NSArray *)keyArray
It is easy to ignore the third, delete a set of keys.
Example
NSDictionary * dic = @{@(1):@"first", @(2):@"second", @(3):@"thrid"}; [[NSMutableDictionary alloc] initWithDictionary:dic]; [mutableDic removeObjectsForKeys:@[@(1),@(2)]]; NSLog(@"%@",mutableDic.description);
Output
2015-02-09 22:34:13.112 DictonaryExample[1273:32793] { 3 = thrid;}
BTY: Years ago plans to update a detailed explanation of KVC and KVO. Then continue to update the fifth chapter of the GCD series. If you have enough energy, update a swift-related one.
IOS SDK Detailed nsdictionary