本文首发于我的个人博客:『不羁阁』 https://bujige.net 文章链接:https://bujige.net/blog/iOS-Foundation-Dictionary.html
本文对Foundation框架中的字典类(NSDictionary和NSMutableDictionary)的使用做一个详细的总结。
+ (instancetype)dictionary;
+ (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
+ (id)dictionaryWithContentsOfFile:(NSString *)path;
+ (id)dictionaryWithContentsOfURL:(NSURL *)url;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Walkers", @"name", @"12345678", @"phone", @"ZhongGuo", @"address", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"Walkers",@"30",@"1.75"] forKeys:@[@"name",@"age",@"height"]];
NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
[dict objectForKey:@"name”];
dict[@"name”];
- (NSUInteger)count;
NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
NSLog(@"count = %lu",[dict count]);
输出结果:count = 3
- (id)objectForKey:(id)aKey;
NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
NSLog(@"%@",[dict objectForKey:@"name"]);
输出结果:Walkers
NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
for (NSString *key in dict) {
NSLog(@"key = %@, value = %@", key, dict[key]);
}
输出结果:
key = name, value = Walkers
key = phone, value = 12345678
key = address, value = ZhongGuo
[dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
NSLog(@"key = %@, value = %@", key, obj);
}];
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
BOOL flag = [dict writeToFile:@"/Users/Walkers/Desktop/dict.plist" atomically:YES];
NSLog(@"flag = %i", flag);
输出结果:flag = 1
文件里的内容显示结果如下图
1.png
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/Walkers/Desktop/dict.plist"];
NSLog(@"newDict = %@", newDict);
// 数组
@[@"Walkers", @"Rose"] (返回是不可变数组)
// 字典
@{ @"name" : @"Walkers", @"phone" : @"12345678" } (返回是不可变字典)
// 数组
id d = array[1];
// 字典
id d = dict[@"name"];
// 数组
array[1] = @"Walkers";
// 字典
dict[@"name"] = @"Walkers";
- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"Walkers" forKey:@"name"];
NSLog(@"%@", dict);
输出结果:
{
name = Walkers;
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"Walkers" forKey:@"name"];
NSLog(@"%@", dict);
[dict setObject:@"abc" forKey:@"name"];
NSLog(@"%@", dict);
输出结果:
{
name = Walkers;
}
{
name = abc;
}
- (void)removeObjectForKey:(id)aKey;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValuesForKeysWithDictionary: @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"}];
[dict removeObjectForKey:@"name"];
NSLog(@"%@",dict);
输出结果:
{
address = ZhongGuo;
phone = 12345678;
}
- (void)removeAllObjects;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValuesForKeysWithDictionary: @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"}];
[dict removeAllObjects];
NSLog(@"%@",dict);
输出结果:
{
}
[dict setObject:@"Jack" forKey:@"name”];
dict[@"name"] = @"Jack";