1.补充
(1).容器的内存管理.
Person *person = [Person alloc] init];
NSMutableArray *nsMA = [NSMutableArray alloc] init];
[nsMA addObject:person];
[nsMA addObject:person];
NSLog(@"%ld", person.retainCount);
(结果:person的引用计数为:3);
[nsMA removeObject:person];
[nsMA release];
NSLog(@"%ld", person.retainCount);
(结果:此时的person的引用计数为:1);
[person release];
(2). 容器的内存管理注意要点.
①.容器的引用计数和容器内的元素无关.
②.但是,当一个元素进入容器时,容器会对该元素的引用计数+1,当元素移除时,引用计数-1,当容器销毁时,容器内部的所有元素,引用计数逐个-1.
2.不可变字典
(1).NSDictionary
key - value 键值对
保存的value,一定是对象.
key,一定唯一.
NSDictionary *nsD = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
NSLog(@"%@", nsD);
![打印结果](https://img-blog.csdn.net/20150909212227905)
(2).字面量
NSDictionary *nsD2 = @{@“key1”: @“value1”, @“key2”: @“value2”};
NSLog(@“%@“, nsD2);
![打印结果](https://img-blog.csdn.net/20150909212504236)
(3).通过key数组和value数组进行创建.
NSArray *keyArr = @[@"a", @"b"];
NSArray *valueArr = @[@"aa", @"bb"];
NSDictionary *dic = [NSDictionary dictionaryWithObjects:valueArr forKeys:keyArr];
NSLog(@"%@", dic);
![打印结果](https://img-blog.csdn.net/20150909212821524)
(4).获取所有的key.
NSLog(@"allKeys:%@", dic.allKeys);
![打印结果](https://img-blog.csdn.net/20150909213055503)
(5).获取所有的value.
NSLog(@"allValue:%@", dic.allValues);
![打印结果](https://img-blog.csdn.net/20150909213218210)
(6).通过key访问value.
NSLog(@"%@", [dic objectForKey:@"b"]);
(结果:b所对应的value为:bb);
(7).通过字面量访问.
NSLog(@"%@", dic[@"a"]);
(结果:a所对应的value为:aa);
(8).获取键值对的个数.
NSLog(@"%lu %lu %lu", dic.allValues.count, dic.allKeys.count, dic.count);
(7).字典的遍历.
普通版.
for (NSInteger i = 0; i < dic.count; i++) {
NSString *key = dic.allKeys[i];
NSLog(@"%@ = %@", key, dic[key]);
}
![遍历结果](https://img-blog.csdn.net/20150909214210470)
进阶版.
for (NSString *key in dic.allKeys) {
NSLog(@“%@ = %@“, key, dic[key]);
}
![遍历结果](https://img-blog.csdn.net/20150909214402708)
完整版
for (NSString *key in dic) {
NSLog(@"%@ = %@", key, [dic objectForKey:key]);
}
![遍历结果](https://img-blog.csdn.net/20150909214508208)
3.可变字典
(1) NSMutableDictionary
NSMutableDictionary *nsMD = [NSMutableDictionary dictionary];
(2).添加 & 修改
①.当key不存在时,添加一对新的键值对.
Person *person = [[[Person alloc] init] autorelease];
[nsMD setObject:person forKey:@"person"];
NSLog(@"%@", nsMD);
![添加结果](https://img-blog.csdn.net/20150909215223245)
②.当key存在时,替换之前的value.
[nsMD setObject:@"*****" forKey:@"person1"];
[nsMD setObject:@"A" forKey:@"name"];
[nsMD setObject:@"B" forKey:@"sex"];
NSLog(@"%@", nsMD);
![添加结果](https://img-blog.csdn.net/20150909215437200)
(3).移除.
[nsMD removeObjectForKey:@"sex"];
NSLog(@"%@", nsMD);
![移除后的结果](https://img-blog.csdn.net/20150909215641450)
(4).移除所有对象.
[nsMD removeAllObjects];
NSLog(@"%@", nsMD);
![移除后的结果](https://img-blog.csdn.net/20150909215835647)
4.嵌套
(1).数组套数组.
NSArray *array1 = @[@"teacher", @"student", @"pencil"];
NSArray *array2 = @[@"soldier", array1];
NSArray *array3 = @[@"AAA", array2, @"BBB"];
NSLog(@"%@", array3);
![结果](https://img-blog.csdn.net/20150910083645828)
(2).字典套字典.
NSDictionary *dic1 = @{@"name": @"Dargon Ball"};
NSDictionary *dic2 = @{@"price": @"100", @"dic": dic1};
NSDictionary *dic3 = @{@"information": dic2, @"rating":@"200"};
NSLog(@"%@", dic3);
![结果](https://img-blog.csdn.net/20150910083746569)
(3).数组套字典.
NSArray *array4 = @[@"name", @"sex"];
NSDictionary *dic4 = @{@"rating": @"10"};
NSArray *array5 = @[array4, dic4];
NSLog(@"%@", array5);
![结果](https://img-blog.csdn.net/20150910083834469)
(4).字典套数组.
NSDictionary *dic5 = @{@"1": array1, @"2": array2, @"3": array5};
NSLog(@"%@", dic5);
![结果](https://img-blog.csdn.net/20150910083931077)
5.数组排序
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"cat", @"dog", @"pig", @"tiger", @"lion", nil];
(1).冒泡排序
for (NSInteger i = 0; i < array.count - 1; i++) {
for (NSInteger j = 0; j < array.count - i - 1; j++) {
if ([array[j] compare:array[j+1]] ==1) {
[array exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
}
}
}
NSLog(@"%@", array);
![结果](https://img-blog.csdn.net/20150910084356024)
(2).不可变数组排序.
![结果](https://img-blog.csdn.net/20150910084553694)
(3).可变数组排序.
[array sortUsingSelector:@selector(compare:)];
NSLog(@"%@",array);
![结果](https://img-blog.csdn.net/20150910084809876)
main.m
6.KVC. key - value - coding 键值对编码.
作用:赋值(字典->其他类的对象)
使用KVC的方法赋值,必须要在相关的类中写属性,并且key值要和属性名对应.
NSDictionary *dic = @{@"id":@"碟中谍5",
@"rating": @8.9,
@"stars": @"Tom",
@"date": @"2015-9-9",
@"collection": @25000,
@"during": @"135min"};
Movie *mv = [[[Movie alloc] init] autorelease];
Movie.h
@interface Movie : NSObject
@property(nonatomic, retain)NSString *name;
@property(nonatomic, retain)NSNumber *rating;
@property(nonatomic, retain)NSString *stars;
@property(nonatomic, retain)NSString *date;
@property(nonatomic, retain)NSNumber *collection;
@property(nonatomic, retain)NSString *during;
@end
Movie.m
@implementation Movie
如果使用KVC的方法赋值时key值没有对应上类文件中的属性,就要写一个纠错方法,该方法自动使key值与属性相对应,从而达到赋值的目的.
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
if ([key isEqualToString:@"id"]) {
[self setValue:@"碟中谍5" forKey:@"name"];
}
}
main.m
(1).KVC赋值.
[mv setValue:@"碟中谍5" forKey:@"name"];
NSLog(@"%@", mv.name);
(结果:碟中谍5)
(2).KVC取值.
NSLog(@"%@, %@", mv.name, [mv valueForKey:@"name"]);
(结果:碟中谍5,碟中谍5).
(3).把mv中全部赋值.
Movie.m
- (NSString *)description {
NSString *str = [NSString stringWithFormat:@"name:%@, stars:%@, collection:%@, rating:%@, date:%@, during:%@", self.name, self.stars, self.collection, self.rating, self.date, self.during];
return str;
}
main.m
[mv setValuesForKeysWithDictionary:dic];
NSLog(@"%@",dic);
![打印结果](https://img-blog.csdn.net/20150910091721603)