How to split a string into NSMutableArray

I want to split a string into NSMutableArray

I know

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

but this one for NSArray not for NSMutableArray .

I need it, because after the split I want to remove an element from the array using

 -(void)removeObjectAtIndex:(NSUInteger)index 

which is not possible in NSArray.

thanks

+6
source share
3 answers

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

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

Also, remember that a copy, such as alloc, allocates new memory. Therefore, when used in code other than ARC, you must autorelease copied array or manually release it when you are done with it.

+28
source

Make a new NSMutableArray with

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

Create an NSMutableArray from the output NSArray created by SeparatedByString components.

 NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithArray:array]; 
+2
source

Source: https://habr.com/ru/post/885516/


All Articles