gpt4 book ai didi

objective-c - 从 NSMutableArray 中删除对象

转载 作者:行者123 更新时间:2023-12-02 11:38:07 25 4
gpt4 key购买 nike

我试图删除索引 1 处的对象,但代码无法编译。

我也不明白这一点:我将“iphone”字符串设置为索引 0,之后我将其从索引 0 中删除,但输出仍然首先显示“iphone”。谁能帮我解释一下吗?

int main (int argc, const char * argv[])
{
@autoreleasepool {

//create three string objetc
NSString *banana = @"This is banana";
NSString *apple = @"This is apple";
NSString *iphone =@"This is iPhone";

//create an empty array
NSMutableArray *itemList = [NSMutableArray array];

// add the item to the array
[itemList addObject:banana];
[itemList addObject:apple];

// put the iphone to the at first

[itemList insertObject:iphone atIndex:0];

for (NSString *l in itemList) {
NSLog(@"The Item in the list is %@",l);
}
[itemList removeObject:0];
[itemList removeObject:1];// this is not allow it

NSLog(@"now the first item in the list is %@",[itemList objectAtIndex:0]);
NSLog(@"now the second time in the list is %@",[itemList objectAtIndex:1]);
NSLog(@"now the thrid item in the list is %@",[itemList objectAtIndex:2]);

}
return 0;
}

最佳答案

应该是这样

[itemList removeObjectAtIndex:0];
[itemList removeObjectAtIndex:1];

这个方法在NSMutableArray的文档中有明确的说明。在提出问题之前,请务必查阅适当的文档。

关于objective-c - 从 NSMutableArray 中删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com