对象编辑数组

时间:2015-01-20 18:13:25

标签: objective-c arrays

我有一个这样的数组:

array[0] = @"object0"
array[1] = @"object1"
array[2] = @"object2"
array[3] = @"object3"

和: 1.如何删除一个对象,例如array [1]并得到它:

array[0] = @"object0"
array[1] = @"object1"
array[2] = @"object2"

2.如何将对象移动到其他索引,例如3-> 1

array[0] = @"object0"
array[1] = @"object3"
array[2] = @"object2"
array[3] = @"object1"

2 个答案:

答案 0 :(得分:0)

尝试NSMutableArray:

// creation array
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:@[@"object0",@"object1",@"object2",@"object3"]];

// remove object        
[array removeObject:@"object0"];

要在其他索引处移动,请查看此处:NSMutablearray move object from index to index

答案 1 :(得分:0)

NSMutableArray *array = [@[@"obj0",
                           @"obj1",
                           @"obj2",
                           @"obj3",
                           @"obj4"] mutableCopy];

// Remove object at Index 1
[array removeObject:array[1]];
NSLog(@"%@", array);

// Move item to new index
id object = array[3];
[array removeObjectAtIndex:3];
[array insertObject:object atIndex:1];
NSLog(@"%@", array);