【问题标题】:Parse RTF file into NSArray - objective C将 RTF 文件解析为 NSArray - 目标 C
【发布时间】: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


【解决方案1】:

尝试使用纯文本 (txt) 文件而不是 rtf。 RTF 文件也包含有关文本的格式信息。那是您阅读内容后看到的不必要的东西。

【讨论】:

  • 如何将 rtf 文档转换为 txt 文档?我可以只更改以 finder 结尾的文件吗?那行得通吗?我会试一试,然后回复你。
  • 如果您使用的是 Mac,请在 TextEdit 中打开文件并按 Cmd+Shift+T(这会将其转换为纯文本并删除所有格式)然后另存为 txt。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 2012-03-19
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
相关资源
最近更新 更多