Pooja
Pooja

Reputation: 2200

how to split string into NSMutableArray

I want to split string into NSMutableArray

I know

- (NSArray *)componentsSeparatedByString:(NSString *)separator

is available but this one is for NSArray not for NSMutableArray.

I need because after spliting i want to remove element from array by using

-(void)removeObjectAtIndex:(NSUInteger)index

which is not possible with NSArray.

Thank you

Upvotes: 8

Views: 13217

Answers (3)

GendoIkari
GendoIkari

Reputation: 11914

You can also just get a mutable copy of the returned array:

NSMutableArray *array = [[myString componentsSeparatedByString:@"..."] mutableCopy];

Also, remember that copy, like alloc, does allocate new memory. So when used in non-ARC code you must autorelease the copied array or manually release it when you are done with it.

Upvotes: 30

Tomas McGuinness
Tomas McGuinness

Reputation: 7691

Create a NSMutableArray from the output NSArray created by componentsSeparatedByString.

NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:array]; 

Upvotes: 2

mvds
mvds

Reputation: 47034

Make a new NSMutableArray using

NSArray *array = [NSString componentsSeparatedByString:@"..."];
NSMutableArray *mutable = [NSMutableArray arrayWithArray:array];

Upvotes: 9

Related Questions