Foundation首页投稿(暂停使用,暂停投稿)iOS进阶指南

OC知识--Foundation框架详尽总结之『字典类』

2016-08-07  本文已影响211人  ITCharge

本文首发于我的个人博客:『不羁阁』 https://bujige.net
文章链接:https://bujige.net/blog/iOS-Foundation-Dictionary.html

本文对Foundation框架中的字典类(NSDictionary和NSMutableDictionary)的使用做一个详细的总结。

1. NSDictionary

1.NSDictionar介绍

2.NSDictionary的创建

+ (instancetype)dictionary;
+ (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;
+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
+ (id)dictionaryWithContentsOfFile:(NSString *)path;
+ (id)dictionaryWithContentsOfURL:(NSURL *)url;

3. NSDictionary创建和获取简写

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”];

4. 键值对集合的特点

5. NSDictionary的遍历

NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
NSLog(@"count = %lu",[dict count]);

输出结果:count = 3
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);
}];

6. NSDictionary文件操作


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);

7. NSDictionary和NSArray对比

1. NSDictionary和NSArray的区别

2. NSDictionary和NSArray的用法

// 数组
@[@"Walkers", @"Rose"] (返回是不可变数组)
// 字典
@{ @"name" : @"Walkers", @"phone" : @"12345678" } (返回是不可变字典)

// 数组
id d = array[1];
// 字典
id d = dict[@"name"];

// 数组
array[1] = @"Walkers";
// 字典
dict[@"name"] = @"Walkers";


2. NSMutableDictionary

1.NSMutableDictionary介绍

2. NSMutableDictionary的常见操作


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;
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValuesForKeysWithDictionary: @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"}];

[dict removeObjectForKey:@"name"];
NSLog(@"%@",dict);

输出结果:
{
    address = ZhongGuo;
    phone = 12345678;
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValuesForKeysWithDictionary: @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"}];

[dict removeAllObjects];
NSLog(@"%@",dict);

输出结果:
{
}

3. NSMutableDictionary的简写

[dict setObject:@"Jack" forKey:@"name”];
dict[@"name"] = @"Jack";
上一篇 下一篇

猜你喜欢

热点阅读