将NSString转换为NSMutableArray [英] Convert NSString into NSMutableArray

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

问题描述

我正在尝试将NSString转换(或复制?)成NSMutableArray。我想我的问题是我不太了解MutableArray的结构。以我的有限知识,一个数组可能看起来像这样:

I am trying to convert (or copy?) a NSString into a NSMutableArray. I guess my problem is that I don't really understand the structure of a MutableArray. In my limited knowledge, an Array could look like this:

NoteBook = [[NSMutableArray alloc] init];

for (int temp = 0; temp < 3; temp++) {
    [NoteBook insertObject:@"Page" atIndex:temp];
}

这会给我一个 PagePagePage 数组。假设我想打开一个包含PagePagePage的txt文件,但是单词之间用定义的字符串分隔开,这样我就可以将数组中的各个对象分开,例如: Page ---页面末尾- -页面---页面结尾---页面

Which would give me an Array of PagePagePage. Let's assume I wanted to open a txt file which contains PagePagePage, but the words were separated by a defined string so that I can keep the individual objects in my array apart, like so: Page--- end of page ---Page--- end of page ---Page.

现在,我的下一步是从txt文件读取此信息:

Now, my next step would be to read this information from the txt file:

NSString *tempTextOut = [NSString stringWithContentsOfFile:filePath
                                                 encoding:NSUTF8StringEncoding
                                                    error:&error];
NoteBook = [tempTextOut componentsSeparatedByString: @"\n--- end of page ---\n"];

但是,最后一行不起作用,xCode告诉我:不兼容的Objective-C类型分配 struct NSArray *,预期的 struct NSMutableArray *。我不太了解-NSArray和MutableArray应该兼容(因为一个是另一个的子类)。 xcode不应告诉我问题是我一直在尝试将NSString转换为NSMutableArray吗?

However, the last line does not work and I'm told by xCode: Incompatible Objective-C types assigning 'struct NSArray*', expected 'struct NSMutableArray*'. I don't really understand this - NSArray and MutableArray should be compatible (as one is the subclass of the other). Shouldn't xCode tell me that the problem is that I've been trying to convert a NSString into an NSMutableArray?

我可能需要在之前重新设置MutableArray吗?再放一些东西,因为现在它仍然包含 PagePagePage ,这是我在第一步中分配给它的。我以为我的NoteBook可变数组可以简单地替换为字符串,但是我想事实并非如此。

Would I perhaps need to re-set my MutableArray before putting something back into it, because right now, it still contains PagePagePage which I have assigned to it in the first step. I thought my NoteBook mutable array would simply be replaced by the string, but I guess that won't be the case.

我非常感谢在此提供的任何帮助物。谢谢!

I'd very much appreciate any help in this matter. Thanks!

推荐答案

如果要操纵不可变实例,将可变对象分配给相同的不可变类型将导致运行时错误。

Assigning a mutable object to the same immutable type will lead to runtime errors if you want to manipulate the immutable instance.

您可以通过以下方式获取可变副本:

You can get your mutable copy by calling:

NoteBook = [[tempTextOut componentsSeparatedByString: @"\n--- end of page ---\n"] mutableCopy];

如果NoteBook是保留属性,则应通过以下方式为其分配:

If NoteBook is a retained property you should assign to it this way:

self.NoteBook = [[[tempTextOut componentsSeparatedByString: @"\n--- end of page ---\n"] mutableCopy] autorelease];

这样可变的副本就不会被保留下来。您可以像平常一样先释放dealloc方法。

so the mutable copy doesn't get over retained. You can release in your dealloc method then as normal.

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

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