Objective-C的数组比C++,Java的数组强大在于,NSArray保存的对象可以是不同的对象。但只能保存对象,int ,char,double等基本数据类型不能直接保存,需要通过转换成对象才能加入数组。
1、NSArray 不可变数组
[array count] : 数组的长度。
[array objectAtIndex 0]: 传入数组脚标的id 得到数据对象。
[arrayWithObjects; ...] :向数组对象初始化赋值。这里可以写任意对象的指针,结尾必须使用nil。
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- NSObject *obj = [[NSObject alloc]init];
- NSArray *array = [NSArray arrayWithObjects:
- @"a",
- obj,
- @"c",nil];
- NSLog(@"array Count:%lu",[array count]);
- //遍历数组
- for (NSObject *object in array) {
- NSLog(@"数组对象:%@", object);
- }
- [obj release];
- }
- return 0;
- }
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSObject *obj = [[NSObject alloc]init];
NSArray *array = [NSArray arrayWithObjects:
@"a",
obj,
@"c",nil];
NSLog(@"array Count:%lu",[array count]);
//遍历数组
for (NSObject *object in array) {
NSLog(@"数组对象:%@", object);
}
[obj release];
}
return 0;
}
打印结果:
2012-07-09 10:52:17.050 objectiveC[944:403] array Count:3
2012-07-09 10:52:17.052 objectiveC[944:403] 数组对象:a
2012-07-09 10:52:17.052 objectiveC[944:403] 数组对象:<NSObject: 0x7fe479c14110>
2012-07-09 10:52:17.053 objectiveC[944:403] 数组对象:c
查找数组索引对应的对象
NSLog(@"Index 2:%@", [array objectAtIndex:2]);
打印结果:2012-07-09 10:55:16.382 objectiveC[993:403] Index 2:c
2、NSMutableArray可变对象数组
[NSMutableArray arrayWithCapacity:6] :初始化可变数组对象的长度,如果后面代码继续添加数组超过长度6以后NSMutableArray的长度会自动扩充,6是自己可以设置的颗粒度。
[array addObject:...] : 向可变数组尾部添加数据对象。
[array addObjectsFromArray:..] :向可变数组尾部添加一个数组对象。
2.1普通使用:
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- NSObject *obj = [[NSObject alloc]init];
- NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
- [muArray addObject:@"对象1"];
- [muArray addObject:@"对象2"];
- [muArray addObject:@"对象3"];
- [muArray addObject:@"对象4"];
- [muArray insertObject:@"搅局的" atIndex:2];
- [muArray addObject:obj];
- for (NSObject * object in muArray) {
- NSLog(@"数组对象:%@", object);
- }
- [obj release];
- }
- return 0;
- }
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSObject *obj = [[NSObject alloc]init];
NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
[muArray addObject:@"对象1"];
[muArray addObject:@"对象2"];
[muArray addObject:@"对象3"];
[muArray addObject:@"对象4"];
[muArray insertObject:@"搅局的" atIndex:2];
[muArray addObject:obj];
for (NSObject * object in muArray) {
NSLog(@"数组对象:%@", object);
}
[obj release];
}
return 0;
}
打印:
2012-07-09 14:01:08.994 objectiveC[2090:403] 数组对象:对象1 2012-07-09 14:01:08.996 objectiveC[2090:403] 数组对象:对象2 2012-07-09 14:01:08.997 objectiveC[2090:403] 数组对象:搅局的 2012-07-09 14:01:08.997 objectiveC[2090:403] 数组对象:对象3 2012-07-09 14:01:08.998 objectiveC[2090:403] 数组对象:对象4 2012-07-09 14:01:08.998 objectiveC[2090:403] 数组对象:<NSObject: 0x109714110>
NSRange range = NSMakeRange(0,3); 设置一个范围为 0 到 3 之间。
[array removeObject:obj inRange:range] : 设置在一个范围内删除数据,如果这个范围内没有删除的这个对象则不会删除任何东西。例子中因为obj对象在 数组 0 到 3的范围内,所以obj就被删除掉了。
2.2 removeObject和removeObjectIdenticalTo
[array removeObject:(id)] :删除数组中指定元素,根据对象isEqual消息判断。
[array removeObjectIdenticalTo:(id)] : 删除数组中指定元素,根据对象的地址判断
[array removeObjectIdenticalTo:(id) inRange:(NSRange)] : 在指定范围内删除指定的元素。
[array removeObjectAtIndex:(NSUInteger)]:删除数组中指定脚标索引的数据。
[array removeObjectsInArray:(NSArray *)] :删除一个数组的元素。
下面我们主要验证下removeObject removeObjectIdenticalTo这两个方法的区别,
先实验removeObject的使用
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- NSString *str1 = [[NSString alloc] init];
- NSString *str2 = [[NSString alloc] init];
- NSString *str3 = [str1 stringByAppendingFormat:@"字符串"];
- NSString *str4 = [str2 stringByAppendingFormat:@"字符串"];
- NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
- [muArray addObject:@"对象"];
- [muArray addObject:str3];
- [muArray addObject:str4];
- for (NSObject * object in muArray) {
- NSLog(@"数组对象:%@", object);
- }
- if ([str3 isEqual:str4]) {
- NSLog(@"str1 isEqual str2");
- }
- if (str3 == str4) {
- NSLog(@"str1 == str2");
- }
- [muArray removeObject:str3];
- for (NSObject * object in muArray) {
- NSLog(@"数组对象:%@", object);
- }
- [str1 release];
- [str2 release];
- }
- return 0;
- }
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *str1 = [[NSString alloc] init];
NSString *str2 = [[NSString alloc] init];
NSString *str3 = [str1 stringByAppendingFormat:@"字符串"];
NSString *str4 = [str2 stringByAppendingFormat:@"字符串"];
NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
[muArray addObject:@"对象"];
[muArray addObject:str3];
[muArray addObject:str4];
for (NSObject * object in muArray) {
NSLog(@"数组对象:%@", object);
}
if ([str3 isEqual:str4]) {
NSLog(@"str1 isEqual str2");
}
if (str3 == str4) {
NSLog(@"str1 == str2");
}
[muArray removeObject:str3];
for (NSObject * object in muArray) {
NSLog(@"数组对象:%@", object);
}
[str1 release];
[str2 release];
}
return 0;
}
运行打印:
2012-07-09 14:57:52.059 objectiveC[2399:403] 数组对象:对象 2012-07-09 14:57:52.061 objectiveC[2399:403] 数组对象:字符串 2012-07-09 14:57:52.062 objectiveC[2399:403] 数组对象:字符串 2012-07-09 14:57:52.062 objectiveC[2399:403] str1 isEqual str2 2012-07-09 14:57:52.063 objectiveC[2399:403] 数组对象:对象
这时,数组里的两个“字符串”对象都被去除了。
现在试试 removeObjectIdenticalTo
代码改成: [muArray removeObjectIdenticalTo:str3];
打印:
2012-07-09 14:59:53.520 objectiveC[2432:403] 数组对象:对象
2012-07-09 14:59:53.521 objectiveC[2432:403] 数组对象:字符串
2012-07-09 14:59:53.522 objectiveC[2432:403] 数组对象:字符串
2012-07-09 14:59:53.523 objectiveC[2432:403] str1 isEqual str2
2012-07-09 14:59:53.523 objectiveC[2432:403] 数组对象:对象
2012-07-09 14:59:53.524 objectiveC[2432:403] 数组对象:字符串
数组中有个还有一个“字符串”留下来了。证明removeObjectIdenticalTo是通过对象的地址做判断的,
我们用对比两个对象isEqual,用==对比对象的地址,打印出来了str1 isEqual str2
inRange的使用
NSRange rang = NSMakeRange(0, 3); [muArray removeObject:obj inRange:rang];
指定范围删除对象,其他方法也可以指定范围删除,如果对象不在这个范围内,则不会被删除。
2.3替换某索引值对应的对象
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- NSObject *obj = [[NSObject alloc]init];
- NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
- [muArray addObject:@"对象1"];
- [muArray addObject:@"对象2"];
- [muArray addObject:@"对象3"];
- [muArray addObject:@"对象4"];
- [muArray addObject:obj];
- for (NSObject * object in muArray) {
- NSLog(@"数组对象:%@", object);
- }
- [muArray replaceObjectAtIndex:4 withObject:@"字符串替换回来"];
- for (NSObject * object in muArray) {
- NSLog(@"数组对象:%@", object);
- }
- [obj release];
- }
- return 0;
- }
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSObject *obj = [[NSObject alloc]init];
NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
[muArray addObject:@"对象1"];
[muArray addObject:@"对象2"];
[muArray addObject:@"对象3"];
[muArray addObject:@"对象4"];
[muArray addObject:obj];
for (NSObject * object in muArray) {
NSLog(@"数组对象:%@", object);
}
[muArray replaceObjectAtIndex:4 withObject:@"字符串替换回来"];
for (NSObject * object in muArray) {
NSLog(@"数组对象:%@", object);
}
[obj release];
}
return 0;
}
替换后的打印结果:
2012-07-09 15:06:01.919 objectiveC[2497:403] 数组对象:对象1 2012-07-09 15:06:01.920 objectiveC[2497:403] 数组对象:对象2 2012-07-09 15:06:01.920 objectiveC[2497:403] 数组对象:对象3 2012-07-09 15:06:01.921 objectiveC[2497:403] 数组对象:对象4 2012-07-09 15:06:01.921 objectiveC[2497:403] 数组对象:字符串替换回来
3、数组迭代的遍历方法
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- NSObject *obj = [[NSObject alloc]init];
- NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
- [muArray addObject:@"对象1"];
- [muArray addObject:@"对象2"];
- [muArray addObject:@"对象3"];
- [muArray addObject:@"对象4"];
- [muArray addObject:obj];
- NSEnumerator *enmuerator = [muArray objectEnumerator];
- id object;
- while (object = [enmuerator nextObject]) {
- NSLog(@"数组中的对象:%@", object);
- }
- [obj release];
- }
- return 0;
- }
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSObject *obj = [[NSObject alloc]init];
NSMutableArray *muArray = [NSMutableArray arrayWithCapacity:6];
[muArray addObject:@"对象1"];
[muArray addObject:@"对象2"];
[muArray addObject:@"对象3"];
[muArray addObject:@"对象4"];
[muArray addObject:obj];
NSEnumerator *enmuerator = [muArray objectEnumerator];
id object;
while (object = [enmuerator nextObject]) {
NSLog(@"数组中的对象:%@", object);
}
[obj release];
}
return 0;
}
打印结果:
2012-07-09 16:00:26.153 objectiveC[2691:403] 数组中的对象:对象1
2012-07-09 16:00:26.154 objectiveC[2691:403] 数组中的对象:对象2
2012-07-09 16:00:26.155 objectiveC[2691:403] 数组中的对象:对象3
2012-07-09 16:00:26.155 objectiveC[2691:403] 数组中的对象:对象4
2012-07-09 16:00:26.156 objectiveC[2691:403] 数组中的对象:<NSObject: 0x7f91f8414110>
著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!