【发布时间】:2013-03-30 10:04:38
【问题描述】:
我正在尝试将 RTF 文件中的英语词典解析为数组。
我最初可以让它工作,但我不确定为什么现在不行。
在字典中,每个单词都用换行符 (\n) 分隔。到目前为止我的代码是:
//loading the dictionary into the file and pull the content from the file into memory
NSData *data = [NSData dataWithContentsOfFile:@"wordList.rtf"];
//convert the bytes from the file into a string
NSString *string = [[NSString alloc] initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
//split the string around newline characters to create an array
NSArray *englishDictionary = [string componentsSeparatedByString:@"\n"];
当我尝试 NSLog 时,它只是出现 (null)...
我已经看过了:Objective-C: Reading a file line by line
但无法通过 Yoon Lee 正常工作得到答案。它在末尾打印出两个反斜杠,并且在开头打印出许多不必要的东西!
任何帮助将不胜感激!干杯!
【问题讨论】:
-
尝试使用纯文本 (txt) 文件而不是 rtf。 RTF 文件也包含有关文本的格式信息。
标签: ios objective-c nsarray rtf text-parsing