objective-c – Cocoa-Touch – 将文本文件加载到数组中

objective-c – Cocoa-Touch – 将文本文件加载到数组中,第1张

概述我的代码有什么问题..我希望它能读取像这样的文本文件 项目1 项目2 项目3 项目4 项目5 并将其解析为一个数组,因此每一行都是这样的数组中的一个单独的对象. 检查控制台时它会打印(null) -(void)parseIntoArray{ //parse the files into seprate arrays. allPools = [[NSMutableArray alloc] i 我的代码有什么问题..我希望它能读取像这样的文本文件

项目1

项目2

项目3

项目4

项目5

并将其解析为一个数组,因此每一行都是这样的数组中的一个单独的对象.

检查控制台时它会打印(null)

-(voID)parseIntoArray{ //parse the files into seprate arrays.    allPools = [[NSMutableArray alloc] initWithContentsOffile:@"ALL_POolS_nameS"];    NSLog(@"%@",allPools);}

我将txt文件放在我的项目中并将其复制到目标.

解决方法 首先,您是否可以验证文件是否存在于您所查找的位置且可读?
使用

[[NSfileManager defaultManager] isReadablefileAtPath:aPath];

其次,你的文件中有什么. initWithContentsOffile的行为:

The array representation in the file IDentifIEd by aPath must contain only property List objects (Nsstring,NSData,NSArray,or NSDictionary objects).

您的文件是有效的pList xml文件吗?

InResponse

您不能使用NSArray构造函数initWithContentsOffile:来解析常规文本文件.

相反,您可以将文件内容读入内存并自行解析为数组.对于您可以使用的示例

//pull the content from the file into memoryNSData* data = [NSData dataWithContentsOffile:aPath];//convert the bytes from the file into a stringNsstring* string = [[[Nsstring alloc] initWithBytes:[data bytes]                                            length:[data length]                                           enCoding:NSUTF8StringEnCoding] autorelease];//split the string around newline characters to create an arrayNsstring* delimiter = @"\n";NSArray* items = [string componentsSeparatedByString:delimiter];
总结

以上是内存溢出为你收集整理的objective-c – Cocoa-Touch – 将文本文件加载到数组中全部内容,希望文章能够帮你解决objective-c – Cocoa-Touch – 将文本文件加载到数组中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1033002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存