Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Cocoa Touch-将文本文件加载到数组中_Objective C_Cocoa_Debugging - Fatal编程技术网

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

Objective c Cocoa Touch-将文本文件加载到数组中,objective-c,cocoa,debugging,Objective C,Cocoa,Debugging,我的代码有什么问题。。我想让它读一个文本文件,比如 项目1 项目2 项目3 项目4 项目5 并将其解析为一个数组,这样每一行在数组中都是一个单独的对象 当您检查控制台时,它会打印(空) 我将txt文件放在我的项目中,并将其复制到目标位置。首先,您能否验证该文件是否存在于您正在查看的位置并且可读? 使用 第二,你的档案里有什么。initWithContentsOfFile: 由aPath标识的文件中的数组表示形式必须仅包含属性列表对象(NSString、NSData、NSArray或NSDicti

我的代码有什么问题。。我想让它读一个文本文件,比如

项目1

项目2

项目3

项目4

项目5

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

当您检查控制台时,它会打印
(空)


我将txt文件放在我的项目中,并将其复制到目标位置。

首先,您能否验证该文件是否存在于您正在查看的位置并且可读? 使用

第二,你的档案里有什么。initWithContentsOfFile:

由aPath标识的文件中的数组表示形式必须仅包含属性列表对象(NSString、NSData、NSArray或NSDictionary对象)

您的文件是有效的plist xml文件吗

in响应

不能使用NSArray构造函数initWithContentsOfFile:解析常规文本文件

相反,您可以将文件内容读入内存,然后自己将其解析为数组。作为你的例子,你可以使用

//pull the content from the file into memory
NSData* data = [NSData dataWithContentsOfFile:aPath];
//convert the bytes from the file into a string
NSString* string = [[[NSString alloc] initWithBytes:[data bytes]
                                            length:[data length] 
                                          encoding:NSUTF8StringEncoding] autorelease];

//split the string around newline characters to create an array
NSString* delimiter = @"\n";
NSArray* items = [string componentsSeparatedByString:delimiter];

嗯,我犯了个错误<编码>编码:NSUTF8StringEncoding]]它说它缺少一个
]
之前你缺少一个参数吗?@shorty876:不,他有一个太多了[开始时的角色。哦,哇,我试图自己修复它,但我只是在看结束括号。。。thanksss@Jeremy:Opps,我本来要放一个自动释放,但在我查找UTF8常量时忘记了!谢谢。打印这是…<代码>“[”,“{”,“\“名称”:“阿富汗\”,“,“\”拨号代码\“:“+93\”,”,“代码”:“AF\”,“},,,
[[NSFileManager defaultManager] isReadableFileAtPath:aPath];
//pull the content from the file into memory
NSData* data = [NSData dataWithContentsOfFile:aPath];
//convert the bytes from the file into a string
NSString* string = [[[NSString alloc] initWithBytes:[data bytes]
                                            length:[data length] 
                                          encoding:NSUTF8StringEncoding] autorelease];

//split the string around newline characters to create an array
NSString* delimiter = @"\n";
NSArray* items = [string componentsSeparatedByString:delimiter];