NSMutableArray繼承自NSArray數組
查用方法code
//初始化5個位置 NSMutableArray *marray= [NSMutableArray arrayWithCapacity:5]; //添加一個元素 [marray addObejct:@"a"]; //添加到指定下標處 [marray insertObject:@"b" atIndex:0]; //移除最後一個元素 [marray removeLastObject]; //移除指定元素 [marray removeObect:@"a"]; //移除指定下標元素 [marray removeObejctAtIndex:0]; //添加數組 [marray addObectsFromArray:array]; //替換指定下標元素 [marray replaceObjectAtIndex:0 withObect:@"obj"]; //移除全部元素 [marray removeAllObjects];
遍歷數組繼承
NSArray *array = [NSArray arryWithObecjts:@"a",@"b",nil]; for (int i = 0 ; i < [array length]; i++){ NSString *ele = [array objectAtIndex:i]; } for (NSString *str in array){ NSLog(@"%@",str); } for (id str in array){ NSLog(@"%@",str); }