Explanation of ios sdk-NSDictionary
Original Blog, reprinted, please indicate the source
Blog.csdn.net/hello_hwc
The content of this article is as follows:
1. Overview of NSDictionary and NSMutableDictionary
2. Examples of common attribute methods (this document does not involve uncommon attributes)
An overview of NSDictionary/NSMutableDictionary
NSDictionary provides a key-value data storage method. In general, any object can be used as a key as long as it complies with the NSCopying protocol. Among them, keys cannot be the same (determined by isEqual ). Neither key nor value can be nil. If you want to express an empty value, use NSNull. The value in NSDictionary is unchangeable.
NSMutableDictionary is a subclass of NSDictionary and a variable dictionary.
2. Examples of common NSDictionary attributes
2.1 create and initialize
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;-(NSDictionary *)initWithDictionary:(NSDictionary *);-(NSDictionary *)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;-(NSDictionary *)initWithObjectsAndKeys:(id)firstObject;
Personally, I prefer the latter one. Of course, do not forget the quick creation method.
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 compares whether the two dictionaries have the same content.
NSDictionary * dic1 = @{@"key1":@"1", @"key2":@"2"}; NSDictionary * dic2 = @{@"key2":@"2", @"key1":@"1"}; if ([dic1 isEqualToDictionary:dic2]) { NSLog(@"Equal contents"); }
2.4 objectForKey: And valueForKey are obtained by attributes.
NSDictionary * dic1 = @{@"key1":@"1", @"key2":@"2"}; NSLog(@"%@",[dic1 objectForKey:@"key1"]); NSLog(@"%@",[dic1 valueForKey:@"key2"]);
2.5 allKeys and allValues obtain all key/value
NSDictionary * dic1 = @{@"key1":@"1", @"key2":@"2"}; NSArray * keys = [dic1 allKeys]; NSArray * values = [dic1 allValues];
2.6 enumerateKeysAndObjectsUsingBlock
Here, stop determines whether to stop traversal. NSDictionary * dic1 =@ {@ "key1": @ "1", @ "key2": @ "2"}; [dic1 enumerateKeysAndObjectsUsingBlock: ^ (id key, id obj, BOOL * stop) {NSLog (@ "% @ = >%@", [key description], [obj description]);}];
2.7 sorting
KeysSortedByValueUsingSelector:
KeysSortedByValueUsingComparator:
Keyssortedbyvaluewitexceptions: usingComparator:
Returns the array of Keys, ordered by 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(id obj1, id obj2) { NSString * str1 = obj1; NSString * str2 = obj2; return [str1 compare:str2]; }]; NSLog(@"%@",numsSortedKeys.description); NSLog(@"%@",strSortedKyes.description);
Output
22:04:12. 070 DictonaryExample [1037: 23292] (
1,
2,
3
)
22:04:12. 070 DictonaryExample [1037: 23292] (
"Id_1 ",
"Id_2 ",
"Id_3"
)
2.8 Filter
KeysOfEntriesPassingTest:
Returns a set of keys that comply with the block constraints of the parameter.
NSDictionary * numsDic = @{@(2):@"second", @(1):@"first", @(3):@"thrid"}; NSSet * filteredKyes = [numsDic keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) { BOOL result = NO; NSNumber * numKey = key; if (numKey.integerValue > 2) { result = YES; } return result; }]; NSLog(@"%@",filteredKyes.description);
Output
22:09:50. 800 DictonaryExample [1099: 25241] {(
3
)}
2.9 write to file
WriteToFile: atomically
WriteToURL: atomically
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) 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-often used to debug and output the contents of a dictionary.
I have already provided more examples. I will not repeat them here.
Three NSMutableDictionary additional methods
3.1 add element
- (void)setObject:(id)anObject forKey:(id
)aKey- (void)setValue:(id)value forKey:(NSString *)key- (void)setDictionary:(NSDictionary *)otherDictionary
Note that when using KVC, The key must be NSString. The third function is to delete the previous element and put the otherDictionary element into the current dic.
Example
NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"object",@"key", nil]; NSLog(@"%@",dic.description); [dic setDictionary:@{@"otherKey":@"otherValue"}]; NSLog(@"%@",dic.description);
Output
22:31:21. 417 DictonaryExample [1232: 31666] {
Key = object;
}
22:31:21. 418 DictonaryExample [1232: 31666] {
OtherKey = otherValue;
}
3.2 delete an element
- (void)removeObjectForKey:(id)aKey- (void)removeAllObjects- (void)removeObjectsForKeys:(NSArray *)keyArray
The third key is easy to ignore. delete a group of keys.
Example
NSDictionary * dic = @{@(1):@"first", @(2):@"second", @(3):@"thrid"}; NSMutableDictionary * mutableDic = [[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: We plan to update a detailed explanation of KVC and KVO years ago. Then update the fifth part of The GCD series. If you have enough energy, let's make a new article about Swift.