ios 从文本文件中读取 - 目标 C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16222682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:20:34  来源:igfitidea点击:

Reading from text file - Objective C

iphoneiosobjective-c

提问by Johnrad

I am trying to familiarize myself with objective C, and my current goal is to read a list of items in a text file and store them in a NSString array.

我正在尝试熟悉目标 C,我目前的目标是读取文本文件中的项目列表并将它们存储在 NSString 数组中。

Currently this is what I have:

目前这是我所拥有的:

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"txt"];
NSData* data = [NSData dataWithContentsOfFile:filepath];
NSString* string = [[NSString alloc] initWithBytes:[data bytes]
                                             length:[data length]
                                           encoding:NSUTF8StringEncoding];

NSString* delimiter = @"\n";
listArray = [string componentsSeparatedByString:delimiter];

I am not sure if this matters, but myList.txtis in my Supporting Files.

我不确定这是否重要,但myList.txt在我的支持文件中。

At the moment, I only have one item in my list. However I am unable to store even that 1 item into my listArray.

目前,我的清单中只有一项。但是,我什至无法将那 1 项存储到我的listArray.

I am sure it is something silly that I am missing, I am just new to Objective C.

我确定我错过了一些愚蠢的事情,我只是 Objective C 的新手。

EDIT:I apologize for not mentioning this earlier. I AM NOT receiving any sort of error. My array is just null.

编辑:我很抱歉没有早点提到这一点。我没有收到任何错误。我的数组只是空的。

回答by DBD

I'm going to suggest a little simplification which might solve your problem since I can't say what your problem is. From the information I'm not sure if you are getting the proper file contents when reading it in or not.

我将建议进行一些简化,这可能会解决您的问题,因为我无法说出您的问题是什么。从这些信息中,我不确定您在读入文件时是否获得了正确的文件内容。

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"txt"];
NSError *error;
NSString *fileContents = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];

if (error)
    NSLog(@"Error reading file: %@", error.localizedDescription);

// maybe for debugging...
NSLog(@"contents: %@", fileContents);

NSArray *listArray = [fileContents componentsSeparatedByString:@"\n"];
NSLog(@"items = %d", [listArray count]);  

回答by ElonChan

If the content of the file is just like:

如果文件的内容是这样的:

[{"Title":"20","Cost":"20","Desc":""},{"Title":"10","Cost":"10.00","Desc":""},{"Title":"5","Cost":"5.00","Desc":""}]

try this

尝试这个

-(id)readFromDocumentDBFolderPath:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:appFile])
    {
        NSError *error= NULL;
        NSData* data = [NSData dataWithContentsOfFile:appFile];
        id resultData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        if (error == NULL)
        {
            return resultData;
        }
    }
    return NULL;
}