本文主要是介绍NSMutableArray的用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*--------------- 给数组分配容量----------------*/NSArray *array;array = [NSMutableArray arrayWithCapacity:20];/*-------------- 在数组末尾添加对象----------------*/- (void) addObject: (id) anObject;NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];[array addObject"Four"];NSLog(@"array:%@",array);/*-------------- 删除数组中指定索引处对象----------------*/ -(void) removeObjectAtIndex: (unsigned) index; NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];[array removeObjectAtIndex:1];NSLog(@"array:%@",array);/*------------- 数组枚举---------------*/ - (NSEnumerator *)objectEnumerator;//从前向后NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];NSEnumerator *enumerator;enumerator = [array objectEnumerator];id thingie;while (thingie = [enumerator nextObject]) {NSLog(@"thingie:%@",thingie);}- (NSEnumerator *)reverseObjectEnumerator;//从后向前NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];NSEnumerator *enumerator;enumerator = [array reverseObjectEnumerator];id object;while (object = [enumerator nextObject]) {NSLog(@"object:%@",object);}//快速枚举NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];for(NSString *string in array){NSLog(@"string:%@",string);}
/*--------------- 给数组分配容量----------------*/NSArray *array;array = [NSMutableArray arrayWithCapacity:20];/*-------------- 在数组末尾添加对象----------------*/- (void) addObject: (id) anObject;NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];[array addObject"Four"];NSLog(@"array:%@",array);/*-------------- 删除数组中指定索引处对象----------------*/ -(void) removeObjectAtIndex: (unsigned) index; NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];[array removeObjectAtIndex:1];NSLog(@"array:%@",array);/*------------- 数组枚举---------------*/ - (NSEnumerator *)objectEnumerator;//从前向后NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];NSEnumerator *enumerator;enumerator = [array objectEnumerator];id thingie;while (thingie = [enumerator nextObject]) {NSLog(@"thingie:%@",thingie);}- (NSEnumerator *)reverseObjectEnumerator;//从后向前NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];NSEnumerator *enumerator;enumerator = [array reverseObjectEnumerator];id object;while (object = [enumerator nextObject]) {NSLog(@"object:%@",object);}//快速枚举NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];for(NSString *string in array){NSLog(@"string:%@",string);}
这篇关于NSMutableArray的用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!