I am trying to remove the object at index 1 but the code won't compile.
I also don't understand this: I set "iphone" string to the index 0, after that I remove it from index 0 but the output still displays "iphone" on first. Can anyone can explain it to me?
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;
}
That should be
[itemList removeObjectAtIndex:0];
[itemList removeObjectAtIndex:1];
This method is clearly stated in the documentation for NSMutableArray. Please always consult the proper documentation before asking a question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With