将 NSString 转换为 NSMutableArray
我正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
componentsSeparatedByString:
返回一个普通的不可变 NSArray,而不是 NSMutableArray。您可以将数组传递给[NSMutableArray arrayWithArray:]
或在数组上使用mutableCopy
从中获取可变数组,或者您可以使用addObjectsFromArray: 在现有的 NSMutableArray 上添加对象。
如果您采用
mutableCopy
路线,请记住您有责任对其调用release
或autorelease
。componentsSeparatedByString:
returns a plain immutable NSArray, not an NSMutableArray. You can pass the array to[NSMutableArray arrayWithArray:]
or usemutableCopy
on the array to get a mutable array from it, or you can useaddObjectsFromArray:
on an existing NSMutableArray to add objects to it.If you go the
mutableCopy
route, do remember that you are responsible for callingrelease
orautorelease
on it.如果您想操作不可变实例,将可变对象分配给相同的不可变类型将导致运行时错误。
您可以通过调用以下方式获取可变副本:
如果 NoteBook 是保留属性,您应该以这种方式分配给它:
这样可变副本就不会过度保留。然后您可以像平常一样在 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:
If NoteBook is a retained property you should assign to it this way:
so the mutable copy doesn't get over retained. You can release in your dealloc method then as normal.