黑马程序员_IOS开发_Objective-C学习笔记_NSArray/NSMutableArray

1.数组简介

NSArray:用来存储有序数组,它是不可变的(不能插入删除数据元素),也不能存储C语言中的基本数据类型(int、float、double、enum、struct),也不能存nil,如果需要使用基本数据类型,需要先转成Objective-c中的对象,用@包装下。

 

NSMutableArray:它是可变的(可以插入删除数据元素),还能限制元素个数 capacity。当你需要对数组元素进行加减操作的时候,就需要使用NSMutableArray。

 

2.创建数组

//1.array
NSArray * array = [NSArray array];  
//2.arrayWithArray:
NSArray * array1 = [NSArray arrayWithArray:arr];
//3.arrayWithObject:
NSArray * array3 = [NSArray arrayWithObject:@"IOS"];
//4.arrayWithObjects:
NSArray * array4 = [NSArray arrayWithObjects:@"IOS", @"Android", nil];
//5.arrayWithObjects:count:
NSString * string5[3] = {@"IT", @"hei", @"ma"};
NSArray * array5 = [NSArray arrayWithObjects:string5 count:2]; //获取数量
//6.init
NSArray * array6 = [[NSArray alloc] init];
array6 = @[@"LLL", @"IIII", @"BBBB"];
[array6 release]; //对象需要进行内存释放
//7.initWithArray:
NSArray * array7 = [[NSArray alloc] initWithArray:array6];
[array7 release];
//8.initWithObjects:
NSArray * array8 = [[NSArray alloc] initWithObjects:array6, array5, nil];
//9.initWithObjects:count:
NSArray * array9 = [[NSArray alloc] initWithObjects:string5 count:1];
[array9 release];
//10.arrayWithCapacity //初始化可变数组对象的长度
NSMutableArray *array10 = [NSMutableArray arrayWithCapacity:10];
//11.initWithCapacity
NSMutableArray * array11 = [[NSMutableArray alloc] initWithCapacity:10];

 

可以使用下面的方式创建数组,并且无需担心内存

NSArray *array2 = @[@"itheima", @"itcast", @"HM2015"];

 

3.数组删除(NSMutableArray)

//1.removeAllObjects 删除数组中所有的Objects
NSMutableArray *list = [[NSMutableArray alloc] init];
Person *car = [[Person alloc] init];
car.price = 10;
car.color = 1;
Person *car1 = [[Person alloc] init];
car1.price = 120;
car1.color = 12;
[list addObject:car];
[list addObject:car1];
NSLog(@"%@", list);
[list removeAllObjects]; // Crash here
 NSLog(@"%@", list);

//2.removeLastObject 删除最后一个object
[list removeLastObject];
NSLog(@"%@", list);

//3.removeObject: //删除指定Object
[list removeObject:car];
NSLog(@"%@", list);

//4.removeObject:inRange: //在指定范围删除car1
NSRange range = {1, 1};
[list removeObject:car1 inRange:range];
NSLog(@"%@", list);

//5.removeObjectAtIndex:  //删除指定下标的object
[list removeObjectAtIndex:3];

//6.removeObjectsInArray: //删除在数组中出现的Object
NSArray * arr00 = @[car];
[list removeObjectsInArray:arr00];
NSLog(@"%@", list);

//7.removeObjectsInRange: //删除指定范围的Object
[list removeObjectsInRange:range];
NSLog(@"%@", list);
[list release];

 

4.数组修改(NSMutableArray)

//1.arrayByAddingObject 向数组添加值返回新的数组
NSArray * array = @[@"itheima", @"itcast", @"HM2015"];
NSString * string11 = @"new start";
NSLog(@"%@", [array arrayByAddingObject:string11]);

//2.arrayByAddingObjectsFromArray 组合两个数组

NSLog(@"%@", [array arrayByAddingObjectsFromArray:array]);

//3.componentsJoinedByString 数组切成字符串
NSLog(@"%@", [array componentsJoinedByString:@","]);

//4.addObject 向数组添加对象
NSMutableArray * array4 = [NSMutableArray arrayWithObjects:@"itheima", @"itcast", nil];
[array4 addObject:@"CCC"];
NSLog(@"%@", array4);

//5.addObjectsFromArray 向数组中添加格式化数组
[array4 addObjectsFromArray:array];
NSLog(@"%@", array4);

//6.insertObject:atIndex 在指定下标添加对象
[array4 insertObject:@"OOO" atIndex:1];
NSLog(@"%@", array4);

//7.replaceObjectAtIndex:withObject 替换指定下标的元素
[array4 replaceObjectAtIndex:1 withObject:@"ppp"];
NSLog(@"%@", array4);

//8.setArray 替换原数组
NSArray * array8 = @[@"A", @"B", @"C"];
[array4 setArray:array8];
NSLog(@"%@", array4);

//9.exchangeObjectAtIndex:withObjectAtIndex 交换指定下标的元素
[array4 exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"%@", array4);

 

5.数组查询

NSString * string = @"CCC";
NSArray * array = @[str];
//1.containsObject   是否包含指定对象
if ([array containsObject:string]) {
    NSLog(@"array 包含 string");
}

//2.count  数组中所含元素个数,不包括 nil
NSArray * array2 = [NSArray arrayWithObjects:@"AAA", @"BBBB", nil];
NSLog(@"count = %zi", array2.count);

//3.getObjects:range:
//4.firstObject 返回两个数组中第一个相同的对象元素
NSMutableArray * array4 = [NSMutableArray arrayWithObjects:@"aaa", @"bbbb", nil];
NSLog(@"%@", [array4 firstObjectCommonWithArray:array4]);

//5.lastObject  返回数组最后一个元素
NSMutableArray * array5 = [NSMutableArray arrayWithObjects:@"aaa", @"bbbb", @"ccc",nil];
NSLog(@"%@", [array5 lastObject]);

//6.objectAtIndex 返回指定索引的元素
NSLog(@"%@", [array5 objectAtIndex:1]);

//7.indexOfObject  返回指定对象的下标
NSLog(@"%li", [array5 indexOfObject:@"bbbb"]);

//8.indexOfObject:inRange 返回范围内指定对象的下标
NSRange range = NSMakeRange(0, 3);
NSLog(@"%li", [array5 indexOfObject:@"ccc" inRange:range]);

//9.subarrayWithRange: 截取范围内的数组元素
NSLog(@"%@", [array5 subarrayWithRange:range]);

 

6.数组比较

//1.isEqualToArray 比较两个数组元素是否相等
if ([array5 isEqualToArray:array5]) {
    NSLog(@"array5 equal array5");
}

 

7.数组遍历

1.for循环遍历
NSUInteger count = array2.count;
for (int i = 0; i < count; i ++) {
    NSLog(@"%@", [array2 objectAtIndex:i]);
}


2.for in 遍历
for (id obj in array2) {
    NSLog(@"%@", obj);
}

 

8.NSArray 文件读写

1.writeToFile写文件,会保存为XML格式的文件。
NSArray * array = @[@"AAA", @"BBB"];
NSString * path = @"/Users/simman/NSarray.xml";
[array writeToFile:path atomically:YES];

2.arrayWithContentsOfFile:path 读文件(规定格式(如:xml)转数组)
NSArray * array1 = [NSArray arrayWithContentsOfFile:path];
NSLog(@"%@", array1);

 

9.数组的内存管理

要说明数组的内存管理,我们可以使用类来做演示。
9.1创建Person类

//创建Person类,并复写dealloc方法,使之能打印出当前释放的对象
- (void)dealloc {
    NSLog(@"%@ is released", self);
    [super dealloc];
}

 

9.2创建三个Person的对象

Person *p1 = [[Person alloc] init];
Person *p2 = [[Person alloc] init];
Person *p3 = [[Person alloc] init];

 

9.3把三个对象放进数组 array 里

NSArray * array = [[NSArray alloc] initWithObjects:p1, p2, p3, nil];

 

9.4打印p1 p2 p3的计数器

NSLog(@"p1_RetainCount = %zi,  p1.retainCount);
NSLog(@"p2_RetainCount = %zi", p2.retainCount);
NSLog(@"p3_RetainCount = %zi", p3.retainCount);

控制台输出:
2015-04-10 16:13:19.012 objective-c——NSArray[11579:303] p1_RetainCount = 2
2015-04-10 16:13:19.014 objective-c——NSArray[11579:303] p2_RetainCount = 2
2015-04-10 16:13:19.016 objective-c——NSArray[11579:303] p3_RetainCount = 2

 

9.5释放p1 p2 p3对象

[p1 release];
[p2 release];
[p3 release];

 

9.6释放array数组

[array release];

2013-10-18 16:01:01.111 objective-c——NSArray[11543:303] &lt;person: 0x10010a2b0> is released
2013-10-18 16:01:01.112 objective-c——NSArray[11543:303] &lt;/person:>&lt;person: 0x100109a50> is released
2013-10-18 16:01:01.112 objective-c——NSArray[11543:303] &lt;/person:>&lt;person: 0x100108c00> is released

 

9.7总结
当把对象放进数组中,该对象将执行retain,计数器将 + 1
当数组 release 的时候,会对所有数组元素做 release 操作。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。