如何格式化要使用 arrayWithContentsOfFile 读取的文本文件

发布于 2024-09-05 05:30:46 字数 521 浏览 3 评论 0原文

我有几个大的单词列表,我已经将它们加载到代码中,如下所示:

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 技术交流群。

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

发布评论

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

评论(5

攒一口袋星星 2024-09-12 05:30:46

myTextFile.txt 应采用 .plist 格式,因此请使用 XCode Plist 编辑器创建它并用数据填充它。

来自文档:

带有内容文件的数组:

  • (id)arrayWithContentsOfFile:(NSString
    *)路径

创建并返回一个数组
包含文件的内容
由aPath指定。该文件
由 aPath 标识的必须包含
产生的字符串表示
writeToFile:atomically: 方法。在
另外,数组表示
必须仅包含属性列表
对象(NSString、NSData、NSArray 或
NSDictionary 对象)。

如果文件不能被返回则返回nil
打开或如果文件的内容
无法解析为数组。

//Use

[array writeToFile:path atomically:YES];

//to write your array directly, or

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];

//or

NSString *error;
NSData *data = [NSPropertyListSerialization dataFromPropertyList:array format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];

//for simpler data (arrays of strings)

//to write from NSData instead

[data writeToFile:path atomically:YES]

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:

arrayWithContentsOfFile:

  • (id)arrayWithContentsOfFile:(NSString
    *)aPath

Creates and returns an array
containing the contents of the file
specified by aPath. The file
identified by aPath must contain a
string representation produced by the
writeToFile:atomically: method. In
addition, the array representation
must contain only property list
objects (NSString, NSData, NSArray, or
NSDictionary objects).

Returns nil if the file can't be
opened or if the contents of the file
can't be parsed into an array.

//Use

[array writeToFile:path atomically:YES];

//to write your array directly, or

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];

//or

NSString *error;
NSData *data = [NSPropertyListSerialization dataFromPropertyList:array format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];

//for simpler data (arrays of strings)

//to write from NSData instead

[data writeToFile:path atomically:YES]
韶华倾负 2024-09-12 05:30:46

只是为了 bShirley 答案的完整性,如果您想使用 pList 编辑器执行此操作:

  1. 在项目中创建一个新的 plist 文件。
  2. 确保将其添加到目标构建阶段设置中的捆绑包中。
  3. 将一个元素添加到 plist,右键单击它并选择 type = array。
  4. 根据需要添加数组元素。
  5. 保存并在文本编辑器中编辑 plist 文件,
  6. 删除 行,对于应以 ...your elements... 结尾的数据,
  7. 通过以下方式加载代码:
    [[NSMutableArray alloc] initWithContentsOfFile:path][NSMutableArray arrayWithContentsofFile:path]。如果您使用第二个,请记住保留或自动释放它。

示例工作 plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"     "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <string>Every 10 Minutes</string>
        <string>Every 20 Minutes</string>
    </array>
</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:

  1. Create a new plist file in your project.
  2. Make sure it is added to your bundle in the target build phase settings.
  3. Add one element to the plist, right click it and select type = array.
  4. Add array elements as needed.
  5. save and then edit the plist file in text editor
  6. remove the <dict>, </dict>, and <key> </key> lines, for the data you should end with <array>...your elements...</array>
  7. load in code via:
    [[NSMutableArray alloc] initWithContentsOfFile:path] or [NSMutableArray arrayWithContentsofFile:path]. Remember to retain or autorelease the second one if you use it.

Example working plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"     "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <string>Every 10 Minutes</string>
        <string>Every 20 Minutes</string>
    </array>
</plist>

this worked for me in Xcode 4.2 on iPad2 with iOS 5.01

这个俗人 2024-09-12 05:30:46

检查 NSArray 的文档:

+ (id)arrayWithContentsOfFile:(NSString *)aPath

参数
路径
包含由 writeToFile:atomically: 方法生成的数组的字符串表示形式的文件的路径。

该文件必须由 writeToFile:atomically: 写入才能从中加载。

Check the documentation for NSArray:

+ (id)arrayWithContentsOfFile:(NSString *)aPath

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.

坦然微笑 2024-09-12 05:30:46

我最终浏览了朋友的代码并找到了我的答案,实际上是一段不错的小代码:

- (NSArray *) readFile:(NSString *) path {


NSString *file = [NSString stringWithContentsOfFile:path];

NSMutableArray *dict = [[NSMutableArray alloc] init];
NSCharacterSet *cs = [NSCharacterSet newlineCharacterSet];
NSScanner *scanner = [NSScanner scannerWithString:file];

NSString *line;
while(![scanner isAtEnd]) {
    if([scanner scanUpToCharactersFromSet:cs intoString:&line]) {
        NSString *copy = [NSString stringWithString:line];
        [dict addObject:copy];

    }
}
NSArray *newArray = [[NSArray alloc] initWithArray:dict];

return newArray;
}

I ended up scavenging through a friends code and found my answer, nice little piece of code actually:

- (NSArray *) readFile:(NSString *) path {


NSString *file = [NSString stringWithContentsOfFile:path];

NSMutableArray *dict = [[NSMutableArray alloc] init];
NSCharacterSet *cs = [NSCharacterSet newlineCharacterSet];
NSScanner *scanner = [NSScanner scannerWithString:file];

NSString *line;
while(![scanner isAtEnd]) {
    if([scanner scanUpToCharactersFromSet:cs intoString:&line]) {
        NSString *copy = [NSString stringWithString:line];
        [dict addObject:copy];

    }
}
NSArray *newArray = [[NSArray alloc] initWithArray:dict];

return newArray;
}
安穩 2024-09-12 05:30:46

如果你在 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!

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