简体   繁体   English

ObjC将文本文件行添加到PickerView

[英]ObjC add text file lines to PickerView

I have this code here, which I'm getting a SIGBRT error on runtime. 我在这里有此代码,在运行时出现SIGBRT错误。 I'm trying to get every line in a text file and add each line into the PickerView, what am I doing wrong? 我正在尝试获取文本文件中的每一行并将每一行添加到PickerView中,我在做什么错呢?

 NSString* path = [[NSBundle mainBundle] pathForResource:@"Documents/Recent Images"
                                                 ofType:@"txt"];


NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];
_imageURLs = @[content, @"Image Two"];

I'm getting the error on the 我在错误

_imageURLs = @[content, @"Image Two"];

Actual error: 实际错误:

2014-09-21 21:04:44.369 app[22995:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]' 2014-09-21 21:04:44.369 app [22995:60b] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[__ NSPlaceholderArray initWithObjects:count:]:尝试从对象中插入零对象[0] “

what am I doing wrong? 我究竟做错了什么?

For any of a number of possible reasons, content is nil . 由于多种可能的原因, contentnil Trying to create the array _imageURLs fails because the first value in the array is nil , and NSArrays cannot contain nil elements. 尝试创建数组_imageURLs失败,因为数组中的第一个值为nil ,并且NSArrays不能包含nil元素。

The first thing you should do is set a breakpoint and confirm for yourself that content really is nil. 您应该做的第一件事是设置一个断点,并自己确认content确实为零。

The second thing you should do is to add a check that ensures that you won't try to create the array if content is nil: 您应该做的第二件事是添加检查,以确保在content为nil时不会尝试创建数组:

if (content) {
    _imageURLs = @[content, @"Image Two"];
}

The third thing that you should do is to figure out why the string you expect isn't being created. 您应该做的第三件事是弄清楚为什么未创建您期望的字符串。 It could be that the path you've provided is wrong. 您提供的路径可能不正确。 Are you sure that there's a directory in your bundle named Documents ? 您确定捆绑包中有一个名为Documents的目录吗? Are you sure the file you're looking for exists there? 您确定要查找的文件在那里吗? Does it contain data that can be read into a string? 它是否包含可以读取为字符串的数据? Using the error parameter in your -stringWithContentsOfFile:... call will help -- that's what it's there for. -stringWithContentsOfFile:...调用中使用error参数将有所帮助-这就是它的用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM