如何格式化要使用 arrayWithContentsOfFile 读取的文本文件
我有几个大的单词列表,我已经将它们加载到代码中,如下所示:
NSArray *dict3 = [[NSArray alloc] initWithObjects:@"abled",@"about",@"above",@"absurd",@"absurdity", ...
但现在它实际上奇怪地导致了 exc_bad_access 错误,我知道这看起来难以置信,但由于某种原因它导致了它。 (相信我,我只是花了一天的大部分时间来调试这个废话)
无论如何,我希望从我的文件中取出这些巨大的代码行,但我不确定最好的方法是什么。我想我可以做一个 plist,但我需要弄清楚如何自动化该过程。
如果我可以只使用我一直在编译的文本文件,那是最简单的,所以如果有人知道如何格式化文本文件,以便
myArray = [NSArray arrayWithContentsOfFile: @"myTextFile.txt"];
将其正确地读取为数组中每个元素一个单词,我将不胜感激。
I have several large word lists and I had been loading them in place in the code as follows:
NSArray *dict3 = [[NSArray alloc] initWithObjects:@"abled",@"about",@"above",@"absurd",@"absurdity", ...
but now it is actually causing an exc_bad_access error weirdly, i know it seems implausible but for some reason IT IS causing it. (trust me on this I just spend the better part of a day debugging this crap)
Anyway, I'm looking to get these humongous lines of code out of my files but I'm not sure what the best approach is. I guess I could do a plist but I need to figure out how to automate the process.
it be easiest if I could just use the text files I've been compiling so if anyone knows how I can format the text file so that the
myArray = [NSArray arrayWithContentsOfFile: @"myTextFile.txt"];
will be read in correctly as one word per element in the array it would really be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
myTextFile.txt
应采用 .plist 格式,因此请使用 XCode Plist 编辑器创建它并用数据填充它。来自文档:
myTextFile.txt
should be in .plist format, so use the XCode Plist editor to create it and fill it up with data.From the docs:
只是为了 bShirley 答案的完整性,如果您想使用 pList 编辑器执行此操作:
、和
。
行,对于应以...your elements...
结尾的数据,[[NSMutableArray alloc] initWithContentsOfFile:path]
或[NSMutableArray arrayWithContentsofFile:path]
。如果您使用第二个,请记住保留或自动释放它。示例工作 plist:
这对我在 iOS 5.01 的 iPad2 上的 Xcode 4.2 中有效
Just for completeness on bShirley's answer, if you wanted to do this using the pList editor:
<dict>
,</dict>
, and<key> </key>
lines, for the data you should end with<array>...your elements...</array>
[[NSMutableArray alloc] initWithContentsOfFile:path]
or[NSMutableArray arrayWithContentsofFile:path]
. Remember to retain or autorelease the second one if you use it.Example working plist:
this worked for me in Xcode 4.2 on iPad2 with iOS 5.01
检查 NSArray 的文档:
参数
路径
包含由 writeToFile:atomically: 方法生成的数组的字符串表示形式的文件的路径。
该文件必须由 writeToFile:atomically: 写入才能从中加载。
Check the documentation for NSArray:
Parameters
aPath
The path to a file containing a string representation of an array produced by the
writeToFile:atomically:
method.The file must be written by
writeToFile:atomically:
in order to load from it.我最终浏览了朋友的代码并找到了我的答案,实际上是一段不错的小代码:
I ended up scavenging through a friends code and found my answer, nice little piece of code actually:
如果你在 Xcode 中创建一个 plist(肯定是在 4.1 中),它将始终是一个字典。如果您想将其作为数组加载,您可以手动编辑该文件(在 TextEdit 中打开它)并删除
。
以及单个条目的键(数组)。您现在可以在 Xcode 中重新打开它并像平常一样编辑它。苹果在这一点上失败了!
If you create a plist in Xcode (definitely in 4.1) it will ALWAYS be a Dictionary. If you want to load it as an Array, you can manually edit the file (open it in TextEdit) and remove the
<dict> </dict>
and the key to your single entry that is an Array. You can now open it back up in Xcode and edit it like normal.Apple fail on this one!