Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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 如何在Xcode中逐行读取支持文件?_Objective C_Xcode_Parsing - Fatal编程技术网

Objective c 如何在Xcode中逐行读取支持文件?

Objective c 如何在Xcode中逐行读取支持文件?,objective-c,xcode,parsing,Objective C,Xcode,Parsing,我目前正在为OSX的Xcode 4.5.1制作一个网页浏览程序,我正在尝试制作一个书签列表。我希望做的是有一个名为Bookmarks.txt的支持文件,其中我会列出如下书签: Google http://www.google.com/ Apple http://www.apple.com/ Microsoft http://www.microsoft.com/ 我已经看过很多讨论这个的页面,但是没有一个适用于我正在做的事情。我现在拥有的是 NSMutableArray *list; NSSt

我目前正在为OSX的Xcode 4.5.1制作一个网页浏览程序,我正在尝试制作一个书签列表。我希望做的是有一个名为Bookmarks.txt的支持文件,其中我会列出如下书签:

Google
http://www.google.com/
Apple
http://www.apple.com/
Microsoft
http://www.microsoft.com/
我已经看过很多讨论这个的页面,但是没有一个适用于我正在做的事情。我现在拥有的是

NSMutableArray *list;

NSString *contents;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Bookmarks" ofType:@"txt"];
if (filePath) {
    content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

    for (NSString *line in [contents componentsSeparatedByString:@"\n"]) {
        [list addObject:line];
    }
}
和戴夫·德隆的方法一样,但我发现戴夫·德隆的方法有各种各样的错误,而这一个什么也没发生。任何帮助都是很好的,但我只是从Xcode起步,知道的很少


谢谢

您的对象未初始化

NSMutableArray *list = [[NSMutableArray alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Bookmarks" ofType:@"txt"];
if (filePath) {
    NSString * content = [NSString initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    for (NSString *line in [contents componentsSeparatedByString:@"\n"]) {
        [list addObject:line];
    }
}

当然,你需要初始化你的数组列表!我可以想象,将其保存为plist文件要比保存为txt文件容易得多,为什么不使用带有书签名->URL的NSDictionary呢?在我看来,你可以用-[NSDictionary initWithContentsOfFile:]加载它,然后用-[NSDictionary writeToFile:Atomicaly:]保存它。如何创建字典文件?它说initWithContentsOfFile不存在。对不起。应该是:NSString*content=[[NSString alloc]initWithContentsOfFile:filePath编码:NSUTF8StringEncoding错误:nil];