将由逗号分隔的NSString转换为NSArray [英] Convert NSString separated by comma to NSArray

查看:127
本文介绍了将由逗号分隔的NSString转换为NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在Objective-C中将NSArray转换为NSString

我有一个NSArray myArray数组,这里是内容:

I have an array of an NSArray myArray, here is the content:

 (
            "<MPConcreteMediaItem: 0x1b7dd0> 8671085923283003246",
            "<MPConcreteMediaItem: 0x1b7e50> 16275751483823231324",
            "<MPConcreteMediaItem: 0x1b7e70> 4976771456698615367"
 )

我使用下面的代码分配给NSString myString:

I used the code below to assign to an NSString myString:

NSString *myString = [myArray description];

输出仍然如预期:

 (
            "<MPConcreteMediaItem: 0x1b7dd0> 8671085923283003246",
            "<MPConcreteMediaItem: 0x1b7e50> 16275751483823231324",
            "<MPConcreteMediaItem: 0x1b7e70> 4976771456698615367"
 )

现在,我想将myString转换回数组名为newArray,我使用了这个:

Now, say I want to convert myString back to an array named newArray, I used this:

NSArray *newArray = [[NSArray alloc]init];
newArray = [myString componentsSeparatedByString:@","];

但是newArray的内容现在不同了:

But the content of the newArray is now different:

(       
"(
    \"<MPConcreteMediaItem: 0x1b7dd0> 8671085923283003246\"",
        "
    \"<MPConcreteMediaItem: 0x1b7e50> 16275751483823231324\"",
        "
    \"<MPConcreteMediaItem: 0x1b7e70> 4976771456698615367\""
)"
) 

我知道我需要做些什么来解决这个问题?

Any idea what I need to do to fix this?

推荐答案

有两种方法

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

使用

NSString *myString = [myArray componentsJoinedByString:@","]; //instead of [myArray description];

这篇关于将由逗号分隔的NSString转换为NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆