Objective-C的数组比C++,Java的数组强大在于,NSArray保存的对象可以是不同的对象。但只能保存对象,int ,char,double等基本数据类型不能直接保存,需要通过转换成对象才能加入数组。
NSArray不可变数组
[array count] : 数组的长度。
[arrayWithObjects; ...] :向数组对象初始化赋值。这里可以写任意对象的指针,结尾必须使用nil
void demoNSArray() {
NSObject *object = [[NSObject alloc] init];
NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",object, nil];
NSLog(@"array的数量是: %ld.",[array count]);
//遍历数组
for(NSObject *obj in array) {
NSLog(@"%@",obj);
}
NSLog(@"%@",[array objectAtIndex:2]);
}
2016-10-10 11:11:03.471886 ArrayDemo[17100:1359352] array的数量是: 4.
2016-10-10 11:11:03.472329 ArrayDemo[17100:1359352] a
2016-10-10 11:11:03.472359 ArrayDemo[17100:1359352] b
2016-10-10 11:11:03.472373 ArrayDemo[17100:1359352] c
2016-10-10 11:11:03.472498 ArrayDemo[17100:1359352] <NSObject: 0x100403630>
2016-10-10 11:11:03.472518 ArrayDemo[17100:1359352] c
NSMutableArray可变数组
[NSMutableArray arrayWithCapacity:6] :初始化可变数组对象的长度,如果后面代码继续添加数组超过长度6以后NSMutableArray的长度会自动扩充,6是自己可以设置的颗粒度。
[array addObject:...] : 向可变数组尾部添加数据对象。
[array addObjectsFromArray:..] :向可变数组尾部添加一个数组对象。
普通使用
void demoNSMutableArray() {
NSObject *object = [[NSObject alloc] init];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:6];
[array addObject:@"1"];
[array addObject:object];
[array addObject:@"2"];
[array addObject:@"3"];
[array addObject:@"4"];
[array insertObject:@"搅局的" atIndex:2]; //插入对象
//遍历数组
for(NSObject *obj in array) {
NSLog(@"%@",obj);
}
NSRange range = NSMakeRange(0, 3);
//object对象在此范围内则删除 否则不删除任何对象
[array removeObject:object inRange:range];
//遍历数组
for(NSObject *obj in array) {
NSLog(@"%@",obj);
}
}
2016-10-10 11:11:03.472518 ArrayDemo[17100:1359352] c
2016-10-10 11:11:03.472556 ArrayDemo[17100:1359352] 1
2016-10-10 11:11:03.472583 ArrayDemo[17100:1359352] <NSObject: 0x100300120>
2016-10-10 11:11:03.472599 ArrayDemo[17100:1359352] 搅局的
2016-10-10 11:11:03.472611 ArrayDemo[17100:1359352] 2
2016-10-10 11:11:03.472621 ArrayDemo[17100:1359352] 3
2016-10-10 11:11:03.472632 ArrayDemo[17100:1359352] 4
2016-10-10 11:11:03.472661 ArrayDemo[17100:1359352] 1
2016-10-10 11:11:03.472673 ArrayDemo[17100:1359352] 搅局的
2016-10-10 11:11:03.472683 ArrayDemo[17100:1359352] 2
2016-10-10 11:11:03.472694 ArrayDemo[17100:1359352] 3
2016-10-10 11:11:03.472704 ArrayDemo[17100:1359352] 4
NSRange range = NSMakeRange(0,3); 设置一个范围为 0 到 3 之间。
[array removeObject:obj inRange:range] : 设置在一个范围内删除数据,如果这个范围内没有删除的这个对象则不会删除任何东西。例子中因为obj对象在 数组 0 到 3的范围内,所以obj就被删除掉了。
删除对象removeObject和removeObjectIdenticalTo
[array removeObject:(id)] :删除数组中指定元素,根据对象isEqual消息判断。
[array removeObjectIdenticalTo:(id)] : 删除数组中指定元素,根据对象的地址判断
[array removeObjectIdenticalTo:(id) inRange:(NSRange)] : 在指定范围内删除指定的元素。
[array removeObjectsInArray:(NSArray *)] :删除一个数组的元素。
void doRemove() {
NSString *str1 = [[NSString alloc] init];
NSString *str2 = [[NSString alloc] init];
NSString *str3 = [str1 stringByAppendingFormat:@"字符串"];
NSString *str4 = [str2 stringByAppendingFormat:@"字符串"];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:6];
[array addObject:@"对象"];
[array addObject:str3];
[array addObject:str4];
for(NSObject *obj in array) {
NSLog(@"数组对象: %@",obj);
}
//比字符串值
if ([str3 isEqual:str4]) {
NSLog(@"str1 == str2");
}
//比对象的地址
if (str3 == str4) {
NSLog(@"str1 == str2");
}
//[array removeObject:(id)] :删除数组中指定元素,根据对象isEqual消息判断,即根据对象的值判断。
// [array removeObject:str3]; //删除所有值为str3的对象
//[array removeObjectIdenticalTo:(id)] : 删除数组中指定元素,根据对象的地址判断
[array removeObjectIdenticalTo:str3]; //只删除str3对象
for(NSObject *obj in array) {
NSLog(@"数组对象: %@",obj);
}
}
2016-10-10 11:19:17.050519 ArrayDemo[17200:1366118] 数组对象: 对象
2016-10-10 11:19:17.050541 ArrayDemo[17200:1366118] 数组对象: 字符串
2016-10-10 11:19:17.050554 ArrayDemo[17200:1366118] 数组对象: 字符串
2016-10-10 11:19:17.050571 ArrayDemo[17200:1366118] isEqual str1 == str2
2016-10-10 11:19:17.050590 ArrayDemo[17200:1366118] 数组对象: 对象
2016-10-10 11:19:17.050642 ArrayDemo[17200:1366118] 数组对象: 字符串
替换某索引值对应的对象
void doReplace() {
NSObject *object = [[NSObject alloc] init];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
[array addObject:@"1"];
[array addObject:@"2"];
[array addObject:@"3"];
[array addObject:@"4"];
[array addObject:object];
for(NSObject *obj in array) {
NSLog(@"数组对象: %@",obj);
}
[array replaceObjectAtIndex:2 withObject:@"被替换的对象"];
for(NSObject *obj in array) {
NSLog(@"数组对象: %@",obj);
}
}
2016-10-10 11:19:17.050666 ArrayDemo[17200:1366118] 数组对象: 1
2016-10-10 11:19:17.050679 ArrayDemo[17200:1366118] 数组对象: 2
2016-10-10 11:19:17.050689 ArrayDemo[17200:1366118] 数组对象: 3
2016-10-10 11:19:17.050700 ArrayDemo[17200:1366118] 数组对象: 4
2016-10-10 11:19:17.050724 ArrayDemo[17200:1366118] 数组对象: <NSObject: 0x1004001d0>
2016-10-10 11:19:17.050737 ArrayDemo[17200:1366118] 数组对象: 1
2016-10-10 11:19:17.050747 ArrayDemo[17200:1366118] 数组对象: 2
2016-10-10 11:19:17.050758 ArrayDemo[17200:1366118] 数组对象: 被替换的对象
2016-10-10 11:19:17.050769 ArrayDemo[17200:1366118] 数组对象: 4
2016-10-10 11:19:17.050813 ArrayDemo[17200:1366118] 数组对象: <NSObject: 0x1004001d0>
数组迭代的遍历方法
void doIterator() {
NSObject *object = [[NSObject alloc] init];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:5];
[array addObject:@"1"];
[array addObject:@"2"];
[array addObject:@"3"];
[array addObject:@"4"];
[array addObject:object];
NSEnumerator *enumearator = [array objectEnumerator];
id obj;
while(obj = [enumearator nextObject]) {
NSLog(@"数组对象: %@",obj);
}
}
2016-10-10 11:19:17.051273 ArrayDemo[17200:1366118] 数组对象: 1
2016-10-10 11:19:17.051300 ArrayDemo[17200:1366118] 数组对象: 2
2016-10-10 11:19:17.051368 ArrayDemo[17200:1366118] 数组对象: 3
2016-10-10 11:19:17.051425 ArrayDemo[17200:1366118] 数组对象: 4
2016-10-10 11:19:17.051491 ArrayDemo[17200:1366118] 数组对象: <NSObject: 0x1004001d0>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。