Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing an object from an NSMutableArray

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;
}
like image 939
Ben Avatar asked May 12 '26 19:05

Ben


1 Answers

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.

like image 90
edc1591 Avatar answered May 14 '26 13:05

edc1591



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!