将 NSString 转换为 NSMutableArray

发布于 2024-10-25 21:17:30 字数 1179 浏览 4 评论 0原文

我正在尝试将 NSString 转换(或复制?)为 NSMutableArray。我想我的问题是我并不真正理解 MutableArray 的结构。根据我有限的知识,数组可能如下所示:

NoteBook = [[NSMutableArray alloc] init];

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

这将为我提供一个 PagePagePage 数组。假设我想打开一个包含 PagePagePage 的 txt 文件,但单词由定义的字符串分隔,以便我可以将数组中的各个对象分开,如下所示: Page--- 页尾 -- -页---页尾---页

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

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

然而,最后一行不起作用,xCode 告诉我: Incomplete Objective-C types allocate 'struct NSArray*', Expected 'struct NSMutableArray*' 。我不太明白这一点 - NSArray 和 MutableArray 应该兼容(因为一个是另一个的子类)。 xCode 不应该告诉我问题是我一直在尝试将 NSString 转换为 NSMutableArray 吗?

在将某些内容放回其中之前,我是否需要重新设置我的 MutableArray,因为现在它仍然包含我在第一步中分配给它的 PagePagePage 。我以为我的 NoteBook 可变数组会简单地被字符串替换,但我想情况并非如此。

我非常感谢在此事上的任何帮助。谢谢!

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];
}

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.

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"];

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?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2024-11-01 21:17:30

componentsSeparatedByString: 返回一个普通的不可变 NSArray,而不是 NSMutableArray。您可以将数组传递给 [NSMutableArray arrayWithArray:] 或在数组上使用 mutableCopy 从中获取可变数组,或者您可以使用 addObjectsFromArray: 在现有的 NSMutableArray 上添加对象。

如果您采用 mutableCopy 路线,请记住您有责任对其调用 releaseautorelease

componentsSeparatedByString: returns a plain immutable NSArray, not an NSMutableArray. You can pass the array to [NSMutableArray arrayWithArray:] or use mutableCopy on the array to get a mutable array from it, or you can use addObjectsFromArray: on an existing NSMutableArray to add objects to it.

If you go the mutableCopy route, do remember that you are responsible for calling release or autorelease on it.

帅气尐潴 2024-11-01 21:17:30

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

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

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

如果 NoteBook 是保留属性,您应该以这种方式分配给它:

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

这样可变副本就不会过度保留。然后您可以像平常一样在 dealloc 方法中释放。

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];

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文