[学习记录]removeObjectAtIndex:删除指定位置的元素

Objective-C中的removeObjectAtIndex:方法:删除指定位置的元素


removeObjectAtIndex:方法的功能是在可变数组中删除指定位置的元素,其语法形式如下:

    - (void)removeObjectAtIndex:(unsigned)index;

    其中,(unsigned)index用来指定要删除元素的位置。以下程序通过使用removeObjectAtIndex:方法,将已创建可变数组中的第3个位置的元素删除。程序代码如下:

    

    #import 
    int main(int argc, const char * argv[])
    {
    @autoreleasepool {
    NSMutableArray *a=[NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",nil];
    NSLog(@"删除前%@",a);
    [a   removeObjectAtIndex:3];//删除位置为3的元素
    NSLog(@"删除后%@",a);
    }
    return 0;
    }

    运行结果如下:

    2013-03-21 00:45:24.611 5.3.8[3001:303] 删除前(
    a,
    b,
    c,
    d,
    e
    )
    2013-03-21 00:45:24.614 5.3.8[3001:303] 删除后(
    a,
    b,
    c,
    e
    )

你可能感兴趣的:(IOS)