Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSArray + remove item from array

Tags:

iphone

nsarray

How to remove an item from NSArray.

like image 367
pratik Avatar asked Nov 13 '09 10:11

pratik


People also ask

What's a difference between NSArray and NSSet?

The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

Can NSArray contain nil?

arrays can't contain nil.


2 Answers

NSArray is not mutable, that is, you cannot modify it. You should take a look at NSMutableArray. Check out the "Removing Objects" section, you'll find there many functions that allow you to remove items:

[anArray removeObjectAtIndex: index]; [anArray removeObject: item]; [anArray removeLastObject]; 
like image 161
luvieere Avatar answered Sep 19 '22 10:09

luvieere


NSMutableArray *arrayThatYouCanRemoveObjects = [NSMutableArray arrayWithArray:your_array];  [arrayThatYouCanRemoveObjects removeObjectAtIndex:your_object_index];  [your_array release];   your_array = [[NSArray arrayWithArray: arrayThatYouCanRemoveObjects] retain]; 

that's about it

if you dont own your_array(i.e it's autoreleased) remove the release & retain messages

like image 38
ahmet emrah Avatar answered Sep 23 '22 10:09

ahmet emrah