将 RTF 文件解析为 NSArray - 目标 C [英] Parse RTF file into NSArray - objective C

查看:109
本文介绍了将 RTF 文件解析为 NSArray - 目标 C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 RTF 文件中的英文字典解析为数组.

I'm trying to parse and English Dictionary from an RTF file into an array.

我最初让它工作,但我不确定为什么现在不行.

I originally had it working, but I'm not sure why it isn't now.

在字典中,每个单词都由一个新行(\n)分隔.到目前为止,我的代码是:

In the dictionary, each word is separated by a new line (\n). My code so far is:

//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)...

When I try and NSLog it, it just comes up with (null)...

我已经看过:Objective-C:阅读逐行文件

但无法通过 Yoon Lee 正常工作得到答案.打印出来的结尾有两个反斜杠,开头还有很多不必要的东西!

But couldn't get the answer by Yoon Lee working properly. It prints out with two back slashes at the end as well as lots of unnecessary stuff at the start!

任何帮助将不胜感激!干杯!

Any help would be greatly appreciated! Cheers!

推荐答案

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

Try using a plain text (txt) file instead of rtf. RTF files contain formatting information about the text as well. Thats the unnecessary stuff that you see after reading the content.

这篇关于将 RTF 文件解析为 NSArray - 目标 C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆