三、实例
1、- (NSUInteger)count; 数组元素个数
NSArray * array;
array = [NSArray arrayWithObjects:@"one", @"two", nil];
NSLog(@"%lu", [array count]);
2013-03-07 09:10:09.610 NSStringDemo[851:303] 2
2、- (id)objectAtIndex:(NSUInteger)index;返回指定索引的数组元素
NSLog(@"%@", [array objectAtIndex:[array count] - 2]);
2013-03-07 09:10:09.613 NSStringDemo[851:303] one
3、- (NSArray *)arrayByAddingObject:(id)anObject;追加对象
NSLog(@"%@", [array arrayByAddingObject:@"demo"]);
2013-03-07 10:16:59.831 NSStringDemo[1196:303] (
one,
two,
demo
)
4、- (NSArray *)arrayByAddingObjectsFromArray:(NSArray *)otherArray; 追加其他数组
NSLog(@"%@", [array arrayByAddingObjectsFromArray:array]);
2013-03-07 10:16:59.832 NSStringDemo[1196:303] (
one,
two,
one,
two
)
5、- (NSString *)componentsJoinedByString:(NSString *)separator; 分割数组为字符串
NSLog(@"%@", [array componentsJoinedByString:@","]);
2013-03-07 10:19:18.054 NSStringDemo[1218:303] one,two
6、- (BOOL)containsObject:(id)anObject; 判断数组是否存在指定元素
NSLog(@"%@", [array containsObject:@"one"]? @"YES" : @"NO");
2013-03-07 22:05:24.151 NSStringDemo[665:303] YES
7、- (NSString *)description; 格式化为一个属性列表
NSLog(@"%@", [array description]);
2013-03-07 22:27:14.740 NSStringDemo[881:303] (
one,
two,
three
)
8、- (NSString *)descriptionWithLocale:(id)locale;本地化格式化为一个属性列表
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"一",@"one" @"二",@"two" @"三",@"three",nil];
NSLog(@"%@", [array descriptionWithLocale: dic]);
2013-03-07 22:28:30.465 NSStringDemo[896:303] (
one,
two,
three
)
9、- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;本地化格式化为一个属性列表(设置缩进)
NSLog(@"%@", [array descriptionWithLocale:dic indent:2]);
2013-03-07 22:28:30.484 NSStringDemo[896:303] (
one,
two,
three
)
10、- (id)firstObjectCommonWithArray:(NSArray *)otherArray; 取出array对象跟demo对象第一个交集元素
NSArray *demo = [[[NSArray alloc] initWithObjects: @"three", @"two", nil] autorelease];
NSLog(@"%@", [array firstObjectCommonWithArray:demo]);
2013-03-08 10:27:00.884 NSStringDemo[676:303] two
11、- (void)getObjects:(id __unsafe_unretained [])objects range:(NSRange)range;返回指定区域的对象
NSArray *demo = [[[NSArray alloc] initWithObjects: @"three", @"two", @"two", nil] autorelease];
id buffer;
[demo getObjects:&buffer range:NSMakeRange(1, 1)];
NSLog(@"%@", [buffer description]);
2013-03-08 20:19:46.637 NSStringDemo[520:303] two
12、- (NSUInteger)indexOfObject:(id)anObject; 获取指定元素的索引
NSLog(@"%lu", [demo indexOfObject:@"two"]);
2013-03-08 20:46:56.487 NSStringDemo[641:303] 1
13、- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;指定区域获取元素的索引
NSLog(@"%lu", [demo indexOfObject:@"three" inRange:NSMakeRange(0, 2)]);
2013-03-08 20:51:05.978 NSStringDemo[757:303] 0
14、- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject; 获取指定元素的索引
NSLog(@"%lu", [demo indexOfObjectIdenticalTo:@"two"]);
2013-03-08 20:58:45.024 NSStringDemo[934:303] 1
15、- (NSUInteger)indexOfObjectIdenticalTo:(id)anObject inRange:(NSRange)range; 指定区域获取指定元素的索引
NSLog(@"%lu", [demo indexOfObjectIdenticalTo:@"two" inRange:NSMakeRange(2, 1)]);
2013-03-08 20:58:45.024 NSStringDemo[934:303] 2
16、- (BOOL)isEqualToArray:(NSArray *)otherArray; 判断二个数组对象是否相等
NSArray *demoTwo = [[[NSArray alloc] initWithObjects:@"one", @"two", @"three", nil] autorelease];
NSLog(@"%d", [demo isEqualToArray:demoTwo]);
2013-03-08 21:35:30.875 NSStringDemo[1051:303] 0
17、- (id)lastObject; 取出数组最后一个元素
NSLog(@"%@",[demoTwo lastObject]);
2013-03-08 21:36:44.834 NSStringDemo[1077:303] three
18、- (NSEnumerator *)objectEnumerator; 返回一个枚举对象
id enumber;
NSEnumerator *enumberators = [demoTwo objectEnumerator];
while((enumber = [enumberators nextObject])) {
NSLog(@"%@", enumber);
}
2013-03-08 21:47:16.341 NSStringDemo[1157:303] one
2013-03-08 21:47:16.342 NSStringDemo[1157:303] two
2013-03-08 21:47:16.342 NSStringDemo[1157:303] three
19、- (NSEnumerator *)reverseObjectEnumerator; 返回一个翻转的枚举对象
NSEnumerator *reversEnumberators = [demoTwo reverseObjectEnumerator];
id reversEnumberator;
while ((reversEnumberator = [reversEnumberators nextObject])) {
NSLog(@"%@", reversEnumberator);
}
2013-03-08 21:50:51.371 NSStringDemo[1205:303] three
2013-03-08 21:50:51.372 NSStringDemo[1205:303] two
2013-03-08 21:50:51.373 NSStringDemo[1205:303] one
20、- (NSData *)sortedArrayHint; 升序排序数组元素
NSLog(@"%@", [[demo sortedArrayHint] description]);
2013-03-08 21:54:12.377 NSStringDemo[1265:303] <bdefa541 45b4711f 45b4711f>
21、- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context; 调用指定方法排序数组元素
22、- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint;调用指定方法排序数组元素
23、- (NSArray *)sortedArrayUsingSelector:(SEL)comparator; 指定比较方法排序数据元素
NSLog(@"%@", [demoTwo sortedArrayUsingSelector:@selector(compare:)]);
2013-03-08 22:21:08.295 NSStringDemo[1385:303] (
one,
three,
two
)
24、- (NSArray *)subarrayWithRange:(NSRange)range; 返回指定区域的数组元素组成新的数组对象
NSLog(@"%@", [demoTwo subarrayWithRange:NSMakeRange(1, 2)]);
2013-03-08 22:14:50.291 NSStringDemo[1323:303] (
two,
three
)
25、- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; 保存数组对象到指定文件
NSString *resourcePath = @"/Users/fantom/work/ios/NSStringDemo/demo.xml";
NSLog(@"%d", [demoTwo writeToFile:resourcePath atomically:YES]);
26、- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically; 保存数组对象到指定URL
NSURL *urlPath = [[NSURL alloc] initFileURLWithPath:[resourcePath stringByExpandingTildeInPath]];
NSLog(@"%d", [demo writeToURL:urlPath atomically:YES]);
27、- (void)makeObjectsPerformSelector:(SEL)aSelector; 每个数组元素都调用指定方法
28、- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument; 每个数组元素都调用指定方法(指定参数)
29、- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexes; 指定索引集合返回新数组
NSLog(@"%@", [demo objectsAtIndexes:[NSIndexSet indexSetWithIndex:0]]);
2013-03-08 22:44:35.168 NSStringDemo[1855:303] (
three
)
1、- (id)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); 返回指定索引数组元素
NSLog(@"%@", [demo objectAtIndexedSubscript:2]);
2013-03-09 08:52:12.206 NSStringDemo[649:303] three
2、- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 数组每个元素执行代码块中的方法(foreach)
NSString *demoString = @"string";
__block BOOL ifFound = NO;
[demo enumerateObjectsUsingBlock:^(id string, NSUInteger idx, BOOL *stop)
{
if([(NSString *)string isEqual:demoString])
{
ifFound = YES;
*stop = YES;
}
return;
}];
NSLog(@"%d", ifFound);
2013-03-09 18:39:53.746 NSStringDemo[599:303] 0
3、- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 指定块使数组每个元素执行代码块中的方法
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"a", @"ab", @"abc", nil];
NSMutableArray *myArrayCount = [NSMutableArray arrayWithCapacity:1];
[myArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
[myArrayCount addObject:[NSNumber numberWithInt:[obj length]]];
}];
NSLog(@"%@", myArrayCount);
2013-03-09 19:12:16.508 NSStringDemo[1903:303] (
1,
2,
3
)
4、- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 遍历数组每个元素执行代码块中的方法
NSString *area = @"Asia";
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
NSMutableArray *areaArray = [NSMutableArray arrayWithCapacity:1];
NSIndexSet *areaIndexes = [timeZoneNames indexesOfObjectsWithOptions:NSEnumerationConcurrent
passingTest:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *tmpStr = (NSString *)obj;
return [tmpStr hasPrefix:area];
}];
[timeZoneNames enumerateObjectsAtIndexes:areaIndexes options:NSEnumerationConcurrent|NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx,BOOL *stop) {
[areaArray addObject:[obj substringFromIndex:[area length]+1]];
}];
NSLog(@"Cities in %@ time zone:%@", area, areaArray);
2013-03-09 19:19:26.138 NSStringDemo[2206:303] Cities in Asia time zone:(
Yerevan,
Yekaterinburg,
Yakutsk,
Vladivostok,
......
)
5、- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); 数组第一个元素对象执行代码块方法(测试用)
__block BOOL ifTrue = NO;
NSUInteger *testUInteger;
testUInteger = [myArray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
if([obj isEqual:@"ab"])
{
ifTrue = YES;
*stop = YES;
return YES;
}
return NO;
}];
NSLog(@"%lu", testUInteger);
2013-03-09 19:37:01.493 NSStringDemo[3044:303] 1
6、- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); 返回通过代码块方法测试的元素索引
NSUInteger *passingTest;
passingTest = [myArray indexOfObjectWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
if([obj isEqual:@"a"] == NO) {
*stop = YES;
return YES;
}
return NO;
}];
NSLog(@"%lu", passingTest);
2013-03-10 07:46:57.721 NSStringDemo[523:303] 1
7、- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);返回通过代码块方法测试的元素索引集第一个索引
NSUInteger *passingTestFirst;
passingTestFirst = [timeZoneNames indexOfObjectAtIndexes:areaIndexes options: NSEnumerationConcurrent passingTest:
^BOOL(id obj, NSUInteger idx, BOOL *stop) {
if([[obj substringFromIndex:[area length]+1] isEqualToString: @"Aden"]) {
*stop = YES;
return idx;
}
return NO;
}];
NSLog(@"%lu", passingTestFirst);
2013-03-10 08:09:42.803 NSStringDemo[1008:303] 212
8、- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); 返回通过代码块方法测试的元素索引集
NSIndexSet *areaIndexs = [timeZoneNames indexesOfObjectsPassingTest:
^(id obj, NSUInteger idx, BOOL *stop)
{
NSString *tmpStr = (NSString *)obj;
return [tmpStr hasPrefix:area];
}];
NSLog(@"%@", areaIndexs);
2013-03-10 08:13:41.411 NSStringDemo[1067:303] <NSIndexSet: 0x102803130>[number of indexes: 78 (in 1 ranges), indexes: (212-289)]
9、- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0); 返回通过指定枚举选项的代码块方法的元素索引集
10、- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);返回通过指定【索引集合《=》枚举选项】的代码块方法的元素索引集
NSIndexSet *areaTwoIndexs = [timeZoneNames indexesOfObjectsAtIndexes:areaIndexs options:NSEnumerationConcurrent passingTest:^(id obj, NSUInteger idx, BOOL * stop)
{
return [[obj substringFromIndex:[area length]+1] hasPrefix:@"A"];
}];
NSLog(@"%@", areaTwoIndexs);
2013-03-10 08:40:23.153 NSStringDemo[1350:303] <NSIndexSet: 0x1004004c0>[number of indexes: 7 (in 1 ranges), indexes: (212-218)]
11、- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0); 返回指定对比方法升序数组
NSArray *demoArray = [NSArray arrayWithObjects:@"53", @"22", @"31", nil];
NSArray *demoArray = [NSArray arrayWithObjects:@"53", @"22", @"31", nil];
NSArray *sortArray = [demoArray sortedArrayUsingComparator:^(id objOne, id objTwo)
{
int oneUInteger = [objOne intValue];
int twoUInteger = [objTwo intValue];
if(oneUInteger > twoUInteger) {
return (NSComparisonResult)NSOrderedDescending;
}
if(oneUInteger < twoUInteger) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(@"%@", sortArray);
NSLog(@"%@", sortArray);
2013-03-10 09:03:03.313 NSStringDemo[1882:303] (
22,
31,
53
)
12、- (NSArray *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);返回指定对比方法升序数组
NSArray *sortedArray = [demoArray sortedArrayWithOptions:NSSortStable usingComparator:^(id objOne , id objTwo)
{
return[objOne compare:objTwo];
}];
NSLog(@"%@", sortedArray);
2013-03-10 09:08:10.676 NSStringDemo[1982:303] (
22,
31,
53
)
1、- (NSUInteger)indexOfObject:(id)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator)cmp NS_AVAILABLE(10_6, 4_0); // binary search指定区域返回通过代码块方法的索引
NSUInteger amount = 9000;
NSNumber* number = [NSNumber numberWithInt:724];
NSMutableArray* anArray = [NSMutableArray arrayWithCapacity:amount];
for (NSUInteger i = 0; i < amount; ++i) {;
[anArray addObject:[NSNumber numberWithUnsignedInteger:i]];
}
NSUInteger index1 = [anArray indexOfObject:number
inSortedRange:NSMakeRange(0, [anArray count])
options:NSBinarySearchingFirstEqual
usingComparator:^(id obj1,id obj2) {
NSInteger iVal1 = [obj1 integerValue];
NSInteger iVal2 = [obj2 integerValue];
if (iVal1 < iVal2)
return NSOrderedAscending;
else if (iVal1 > iVal2)
return NSOrderedDescending;
else
return NSOrderedSame;
}];
NSLog(@"%lu", index1);
2013-03-10 18:49:52.302 NSStringDemo[396:303] 724
2、+ (id)array; 创建一个空数组
NSArray *demo = [NSArray array];
NSLog(@"%@", demo);
2013-03-10 18:51:40.546 NSStringDemo[439:303] (
)
3、+ (id)arrayWithObject:(id)anObject; 指定一个元素创建数组对象
NSLog(@"%@", [NSArray arrayWithObject:@"demo"]);
2013-03-10 18:53:09.747 NSStringDemo[482:303] (
demo
)
4、+ (id)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;指定个数从一个数组对象创建新的数组对象(类消息)
demoString[0] = @"January";
demoString[1] = @"February";
demoString[2] = @"March";
NSArray *demoCount = [NSArray arrayWithObjects:demoString count:2];
NSLog(@"%@", [demoCount description]);
5、+ (id)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 指定多个元素创建数组对象
NSArray *demos = [NSArray arrayWithObjects:@"demoOne", "demoTwo", @"demoThree", nil];
NSLog(@"%@", demos[0]);
2013-03-10 19:01:47.060 NSStringDemo[700:303] (
demo
)
6、+ (id)arrayWithArray:(NSArray *)array;生成新另外一个数组
NSLog(@"%@", [NSArray arrayWithArray:demoCount]);
2013-03-10 19:16:29.434 NSStringDemo[985:303] (
January,
February
)
7、- (id)initWithObjects:(const id [])objects count:(NSUInteger)cnt; 指定个数从一个数组对象创建新的数组对象(实例消息)
NSLog(@"%@", [[NSArray alloc] initWithObjects:demoString count:2]);
2013-03-10 19:25:17.756 NSStringDemo[1218:303] (
January,
February
)
8、- (id)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION; 指定多个元素创建数组对象
NSArray *demoinitObjects = [[NSArray alloc] initWithObjects:@"demoOne", @"demoTwo", @"demoThree", nil];
NSLog(@"%@", [demoinitObjects description]);
2013-03-10 19:24:06.095 NSStringDemo[1195:303] (
demoOne,
demoTwo,
demoThree
)
9、-(id)initWithArray:(NSArray *)array; 从已有数组初始化
NSLog(@"%@", [[[NSArray alloc] initWithArray:demos] autorelease]);
2013-03-10 19:26:33.091 NSStringDemo[1256:303] (
demoOne,
demoTwo,
demoThree
)
10、- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag;从已有数组初始化(标示作用,浅复制,还是深复制)
NSLog(@"%@", [[NSArray alloc] initWithArray:demoCount copyItems:NO]);
2013-03-10 19:27:53.466 NSStringDemo[1295:303] (
January,
February
)
11、+ (id)arrayWithContentsOfFile:(NSString *)path;文件加载数据
12、+ (id)arrayWithContentsOfURL:(NSURL *)url;从网络地址加载数据
13、- (id)initWithContentsOfFile:(NSString *)path;从文件加载数据
14、- (id)initWithContentsOfURL:(NSURL *)url;//从网络地址加载数据
NSString *writeFilePath = @"/Users/fantom/work/ios/NSStringDemo/demo.xml";
NSURL *urlPath = [[NSURL alloc] initFileURLWithPath:[writeFilePath stringByExpandingTildeInPath]];
NSLog(@"%@",[NSArray arrayWithContentsOfFile:writeFilePath]);
NSLog(@"%@",[NSArray arrayWithContentsOfURL:urlPath]);
NSLog(@"%@",[[[NSArray alloc] initWithContentsOfFile:writeFilePath] autorelease]);
NSLog(@"%@",[[[NSArray alloc] initWithContentsOfURL:urlPath] autorelease]);
013-03-10 19:38:47.703 NSStringDemo[1701:303] (
three,
two,
two
)
2013-03-10 19:38:47.706 NSStringDemo[1701:303] (
three,
two,
two
)
2013-03-10 19:38:47.707 NSStringDemo[1701:303] (
three,
two,
two
)
2013-03-10 19:38:47.708 NSStringDemo[1701:303] (
three,
two,
two
)
1、- (void)getObjects:(id __unsafe_unretained [])objects;获取指定区域对象,容易内存泄露
NSArray *mArray = [[NSArray alloc] initWithObjects:@"demoOne", @"demoTwo", @"demoThree", nil];
id *objects;
NSRange range= NSMakeRange(1, 2);
objects = malloc(sizeof(id) * range.length);
[mArray getObjects:objects range:range];
NSLog(@"%@", objects[1]);
free(objects);
2013-03-11 20:24:29.299 NSStringDemo[616:303] demoThree
2、- (void)addObject:(id)anObject; 添加对象
3、- (void)insertObject:(id)anObject atIndex:(NSUInteger)index; 指定索引位置插入对象
4、- (void)removeLastObject;移除最后一个对象
5、- (void)removeObjectAtIndex:(NSUInteger)index;移除指定索引位置对象
6、- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;替换指定索引位置对象
NSMutableArray *demoMutableArray = [[[NSMutableArray alloc] initWithObject:@"demoOne"] autorelease];
[demoMutableArray addObject:@"demoTwo"];
NSLog(@"%@", demoMutableArray);
[demoMutableArray insertObject:@"demoThree" atIndex:2];
NSLog(@"%@", demoMutableArray);
[demoMutableArray removeLastObject];
NSLog(@"%@", demoMutableArray);
[demoMutableArray removeObjectAtIndex:0];
[demoMutableArray replaceObjectAtIndex:0 withObject:@"demoOne"];
NSLog(@"%@", demoMutableArray);
2013-03-11 21:45:22.890 NSStringDemo[1043:303] (
demoOne,
demoTwo
)
2013-03-11 21:45:22.892 NSStringDemo[1043:303] (
demoOne,
demoTwo,
demoThree
)
2013-03-11 21:45:22.893 NSStringDemo[1043:303] (
demoOne,
demoTwo
)
2013-03-11 21:45:22.894 NSStringDemo[1043:303] (
demoOne
)
1、- (void)addObjectsFromArray:(NSArray *)otherArray; 追加数组A到数组B
NSMutableArray * demoMutableArray = [[NSMutableArray alloc] initWithObjects:@"January", @"February", @"March", nil];
NSMutableArray * demoTwoMutableArray = [NSMutableArray arrayWithObject:@"April"];
[demoMutableArray addObjectsFromArray:demoTwoMutableArray];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 19:15:00.212 NSStringDemo[719:303] (
January,
February,
March,
April
)
2、- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2; 交互指定索引之间的对象
[demoMutableArray exchangeObjectAtIndex:1 withObjectAtIndex:3];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:25:53.897 NSStringDemo[923:303] (
January,
April,
March,
February
)
3、- (void)removeAllObjects; 销毁数组
[demoMutableArray removeAllObjects];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:27:14.495 NSStringDemo[971:303] (
)
4、- (void)removeObject:(id)anObject inRange:(NSRange)range;移除指定区域指定对象.根据对象isEqual消息判断。
[demoMutableArray removeObject:@"March" inRange:NSMakeRange(1, 3)];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:29:00.084 NSStringDemo[1038:303] (
January,
April,
February
)
5、- (void)removeObject:(id)anObject;移除指定对象
[demoMutableArray removeObject:@"April"];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:30:35.239 NSStringDemo[1089:303] (
January,
February
)
6、- (void)removeObjectIdenticalTo:(id)anObject inRange:(NSRange)range;移除指定区域指定对象,根据对象的地址判断
[demoMutableArray removeObjectIdenticalTo:@"April" inRange:NSMakeRange(0, 3)];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:32:14.013 NSStringDemo[1134:303] (
January,
March,
February
)
7、- (void)removeObjectIdenticalTo:(id)anObject;移除指定对象
[demoMutableArray removeObjectIdenticalTo:@"January"];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:39:55.732 NSStringDemo[1188:303] (
March,
February
)
8、- (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt NS_DEPRECATED(10_0, 10_6, 2_0, 4_0);移除指定索引之间的对象
NSUInteger i = 0;
NSUInteger u = 2;
[demoMutableArray removeObjectsFromIndices:&i numIndices:u];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:44:19.076 NSStringDemo[1322:303] (
)
9、- (void)removeObjectsInArray:(NSArray *)otherArray;移除数组A包含的指定数组B元素
[demoMutableArray removeObjectsInArray:demoTwoMutableArray];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:46:06.652 NSStringDemo[1376:303] (
January,
March,
February
)
10、- (void)removeObjectsInRange:(NSRange)range;移除指定区域所有对象
[demoMutableArray removeObjectsInRange:NSMakeRange(1, 2)];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:47:16.458 NSStringDemo[1410:303] (
January
)
11、- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray range:(NSRange)otherRange;指定(数组B的指定区域)的元素替换(数组A指定区域)
NSMutableArray * demoThreeMutableArray = [NSMutableArray arrayWithObjects:@"May", @"June", @"July", nil];
[demoMutableArray replaceObjectsInRange:NSMakeRange(1, 3) withObjectsFromArray:demoThreeMutableArray range:NSMakeRange(2, 1)];
NSLog(@"%@", [demoMutableArray description]);
2013-03-12 20:58:04.211 NSStringDemo[1688:303] (
January,
July
)
12、- (void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray;指定数组B替换(数组A指定区域)的元素
[demoMutableArray replaceObjectsInRange:NSMakeRange(1, 1) withObjectsFromArray:demoThreeMutableArray];
NSLog(@"%@", [demoMutableArray description]);
return 0;
}2013-03-12 20:58:04.211 NSStringDemo[1688:303] (
January,
May,
June,
July
)
13、- (void)setArray:(NSArray *)otherArray;指定数组B替换A
NSMutableArray * demoMutableArray = [NSMutableArray arrayWithObjects:@"January", @"February", @"March", nil];
NSArray *demoArray = [NSArray arrayWithObjects:@"April", @"May", nil];
[demoMutableArray setArray:demoArray];
NSLog(@"%@", demoMutableArray);
2013-03-14 08:27:02.297 NSStringDemo[639:303] (
April,
May
)
14、- (void)sortUsingFunction:(NSInteger (*)(id, id, void *))compare context:(void *)context; 指定方法升序比较
15、- (void)sortUsingSelector:(SEL)comparator;指定方法升序比较
16、- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes; 指定索引集合插入数组元素
[demoMutableArray insertObjects:demoArray atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(3, 2)]];
NSLog(@"%@", demoMutableArray);
2013-03-14 08:37:44.107 NSStringDemo[763:303] (
January,
February,
March,
April,
May
)
17、- (void)removeObjectsAtIndexes:(NSIndexSet *)indexes; 移除指定索引集合元素
[demoMutableArray removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 3)]];
NSLog(@"%@", demoMutableArray);
2013-03-14 08:39:25.053 NSStringDemo[812:303] (
April,
May
)
18、- (void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects; 替换指定索引集合的数组元素
[demoMutableArray replaceObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)] withObjects:demoArray];
NSLog(@"%@", demoMutableArray);
2013-03-14 08:44:29.244 NSStringDemo[1071:303] (
January,
April,
May
)
19、- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0); 指定索引替换新的数组元素
[demoMutableArray setObject:demoArray atIndexedSubscript:3];
NSLog(@"%@", demoMutableArray);
2013-03-14 08:46:45.696 NSStringDemo[1161:303] (
January,
April,
May,
(
April,
May
)
)
20、- (void)sortUsingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0); 指定块方法排序数组元素
NSMutableArray * demoThreeArray = [[[NSMutableArray alloc] initWithObjects:@"23", @"3", @"13", nil] autorelease];
[demoThreeArray sortUsingComparator: ^(id objOne, id objTwo) {
NSInteger intOne = [objOne integerValue];
NSInteger intTwo = [objTwo integerValue];
if (intOne> intTwo) {
return (NSComparisonResult)NSOrderedDescending;
}
if (intOne < intTwo) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(@"%@", demoThreeArray);
2013-03-14 09:01:55.568 NSStringDemo[1493:303] (
3,
13,
23
)
21、- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr NS_AVAILABLE(10_6, 4_0);指定块比较数组
[demoThreeArray sortWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(id objOne, id objTwo) {
NSInteger intOne = [objOne integerValue];
NSInteger intTwo = [objTwo integerValue];
if (intOne> intTwo) {
return (NSComparisonResult)NSOrderedAscending;
}
if (intOne < intTwo) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSLog(@"%@", demoThreeArray);
2013-03-14 09:05:50.176 NSStringDemo[1583:303] (
23,
13,
3
)
22、+ (id)arrayWithCapacity:(NSUInteger)numItems; 初始化化数组对象容量
23、- (id)initWithCapacity:(NSUInteger)numItems;初始化化数组对象容量
NSMutableArray *demoOneCapacity = [NSMutableArray arrayWithCapacity:3];
[demoOneCapacity addObject:@"demoOne"];
NSMutableArray *demoTwoCapacity = [[[NSMutableArray alloc] initWithCapacity:3] autorelease];
[demoTwoCapacity addObject:@"demoTwo"];
NSLog(@"%@ --- %@", demoOneCapacity, demoTwoCapacity);
2013-03-14 09:11:59.373 NSStringDemo[1743:303] (
demoOne
) --- (
demoTwo
)