NSMutableArray基础-创建、添加、删除、替换
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 //创建并设定数组元素个数 6 NSMutableArray *arr1=[NSMutableArray arrayWithCapacity:7]; 7 //复制数组 8 NSArray *arr2=@[@"mon",@"tue",@"wed",@"thu",@"fri",@"sat",@"sun"]; 9 NSMutableArray *arr3=[NSMutableArray arrayWithArray:arr2]; 10 //向数组中添加一个元素 11 [arr3 addObject:@"eig"]; 12 //根据指定下标插入元素 13 [arr3 insertObject:@"nin" atIndex:8]; 14 //删除最后一个元素 15 [arr3 removeLastObject]; 16 //根据指定下标删除元素 17 [arr3 removeObjectAtIndex:7]; 18 //根据指定对象删除元素(若未找到返回NSNotfind 19 [arr3 removeObject:@"sun"]; 20 //替换指定下标的元素 21 [arr3 replaceObjectAtIndex:0 withObject:@"one"]; 22 //删除全部元素 23 [arr3 removeAllObjects]; 24 } 25 return 0; 26 }