如何将字符串拆分为NSMutableArray

时间:2023-01-29 21:42:03

I want to split string into NSMutableArray

我想将字符串拆分为NSMutableArray

I know

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

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

可用,但这个是NSArray而不是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.

这是NSArray无法实现的。

Thank you

3 个解决方案

#1


30  

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.

另外,请记住,像alloc这样的副本确实会分配新的内存。因此,当在非ARC代码中使用时,您必须自动释放复制的阵列或在完成后手动释放它。

#2


9  

Make a new NSMutableArray using

使用创建一个新的NSMutableArray

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

#3


2  

Create a NSMutableArray from the output NSArray created by componentsSeparatedByString.

从componentsSeparatedByString创建的输出NSArray创建NSMutableArray。

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

#1


30  

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.

另外,请记住,像alloc这样的副本确实会分配新的内存。因此,当在非ARC代码中使用时,您必须自动释放复制的阵列或在完成后手动释放它。

#2


9  

Make a new NSMutableArray using

使用创建一个新的NSMutableArray

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

#3


2  

Create a NSMutableArray from the output NSArray created by componentsSeparatedByString.

从componentsSeparatedByString创建的输出NSArray创建NSMutableArray。

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