10,创建字典
字典数组也分不可修改NSDictionary和可修改NSMutableDictionary两种。
创建字典如下
NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World", @"Bonjour tout le? monde", @"Hola Mundo", nil];
NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french", @"spanish", nil];
NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjects:listOfObjects
forKeys:listOfKeys];
下面是常用构造方法。
- (id)initWithObjects:(const id [])objects forKeys:(const id [])keys count:(NSUInteger)cnt;
初始化的字典 指定的对象,key值和计数....等等
代码示例。
NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french", @"spanish", nil];
NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjects:listOfObjects
forKeys:listOfKeys];
NSLog(@"dictionary2 = %@", dictionary2);
11,访问字典属性。
接上例子。
NSString *helloWorld = [dictionary2 objectForKey:@"english"];
NSLog(@"%@", helloWorld);
根据key找到相对应的值。
我们测试了,直接用下标方法也可以,且推荐。
NSString *helloWorld = dictionary2[@"english”];
12,获取个数。
其实跟数组一样啦,.count就行。dictionary2.count。
13,遍历字典。
如果用for的话。
for (NSString *s in [dictionary2 allValues]) {
NSLog(@"value: %@", s);
}
这里是指明allValues说明你要遍历值。换成allKeys,说明你要遍历键。
你也可以通过enumerateKeysAndObjectsUsingBlock将键和值全部遍历出来,用法前面讲过,下面是代码示例。
NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french", @"spanish", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:listOfObjects
forKeys:listOfKeys];
for (NSString *s in [dictionary allValues]) {
NSLog(@"value: %@", s);
}
for (NSString *s in [dictionary allKeys]) {
NSLog(@"key: %@", s);
}
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"key = %@ and obj = %@", key, obj);
}];
14,操作字典内容。
比如声明字典然后往里添加值。
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:@"Hello World"
forKey:@"english"];
[dictionary setObject:@"Bonjour tout le monde"
forKey:@"french"];
[dictionary setObject:@"Hola Mundo"
forKey:@"spanish”];
移除值。
[dictionary removeObjectForKey:@"french”];
全部移除用removeAllObjects
下面是示例。
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:@"Hello World"
forKey:@"english"];
[dictionary setObject:@"Bonjour tout le monde"
forKey:@"french"];
[dictionary setObject:@"Hola Mundo"
forKey:@"spanish"];
NSLog(@"OBJECTS ADDED TO DICTIONARY: %@", dictionary);
[dictionary removeObjectForKey:@"french"];
NSLog(@"OBJECT REMOVED FROM DICTIONARY: %@", dictionary);
[dictionary removeAllObjects];
NSLog(@"ALL OBJECTS REMOVED FROM DICTIONARY: %@", dictionary);
15,保存到文件里。
跟保存数组没啥区别,下面是代码。
NSArray *listOfObjects = [NSArray arrayWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
NSArray *listOfKeys = [NSArray arrayWithObjects:@"english", @"french", @"spanish", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:listOfObjects
forKeys:listOfKeys];
NSString *filePathName = @"/Users/Shared/dictionary.txt";
[dictionary writeToFile:filePathName
atomically:YES];
16,读取。
如果你刚刚保存成功了,下面我们试着读取。
NSString *filePathName = @"/Users/Shared/dictionary.txt";
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:filePathName];
NSLog(@"dictionary: %@", dictionary);
注意用initWithContentsOfFile。
17,我们把set翻译成集合,创建集合。
它有这样几个初始化方法。
- (id)initWithObjects:(const id *)objects count:(NSUInteger)cnt;
- (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (id)initWithSet:(NSSet *)set;
- (id)initWithSet:(NSSet *)set copyItems:(BOOL)flag;
- (id)initWithArray:(NSArray *)array;
Description
Initializes a set with the specified objects and count
Initializes a set with the specified nil- terminated list of objects
Initializes a set using another set
Initializes a set using another set and optionally creates new copies of each object
Initializes a set with the specified objects
集合也分不可变集合NSSet和可变集合NSMutableSet
代码都简单的我懒得抄了。
NSSet *set = [NSSet setWithObjects:@"Hello World", @"Bonjour tout le monde",@"Hola Mundo", nil];
NSLog(@"set: %@",set);
18,获取个数,不废话了,用.count.
19,比较集合。
比如用两个集合。
NSSet *set1 = [NSSet setWithObjects:@"A", @"B", @"C", @"D", @"E", nil];
NSSet *set2 = [NSSet setWithObjects:@"D", @"E", @"F", @"G", @"H", nil];
你想看看两个集合里有没有重合的值,a数组里的值是否存在于b数组。。可以用setsIntersect,它返回一个BOOL值。例如
BOOL set2IsSubset = [set2 isSubsetOfSet:set1];
是否是子集用isSubsetOfSet
isEqualToSet测试两个集合是否相同。
某个对象是否存在于集合里面,用containsObject找出来。
代码示例
NSSet *set1 = [NSSet setWithObjects:@"A", @"B", @"C", @"D", @"E", nil];
NSSet *set2 = [NSSet setWithObjects:@"D", @"E", @"F", @"G", @"H", nil];
NSLog(@"set1 contains:%@", set1);
NSLog(@"set2 contains:%@", set2);
BOOL setsIntersect = [set1 intersectsSet:set2];
BOOL set2IsSubset = [set2 isSubsetOfSet:set1];
BOOL set1IsEqualToSet2 = [set1 isEqualToSet:set2];
BOOL set1ContainsD = [set1 containsObject:@"D"];
NSLog(@"setsIntersect = %i, set2IsSubset = %i, set1IsEqualToSet2 = %i, set1ContainsD = %i", setsIntersect, set2IsSubset, set1IsEqualToSet2, set1ContainsD);
20,遍历集合。
用for遍历。
for (NSString *s in [set1 allObjects]) {
NSLog(@"value: %@", s);
}
用block
[set2 enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
NSLog(@"obj = %@", obj);
}];
对每个原色都执行一个方法
[set2 makeObjectsPerformSelector:@selector(description)];
排倒序了。
具体代码。
NSSet *set = [NSSet setWithObjects:@"Hello World", @"Bonjour tout le monde", @"Hola Mundo", nil];
for (NSString *s in [set allObjects]) {
NSLog(@"value: %@", s);
}
[set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
NSLog(@"obj = %@", obj);
}];
[set makeObjectsPerformSelector:@selector(description)];
NSLog(@"%@",set);
21,设置内容。
添加内容。
[set addObject:@"Hello World”];
删除内容
[set removeObject:@"Bonjour tout le monde”];
删除所有内容。
removeAllObjects
示例代码。
NSMutableSet *set = [[NSMutableSet alloc] init];
[set addObject:@"Hello World"];
[set addObject:@"Bonjour tout le monde"];
[set addObject:@"Hola Mundo"];
NSLog(@"Objects added to set:%@", set);
[set removeObject:@"Bonjour tout le monde"];
NSLog(@"Object removed from set:%@", set);
[set removeAllObjects];
NSLog(@"All objects removed from set:%@", set);
对象集合 二