-4

how can I load the content of a txt file divided by lines in a NSArray? Basically, I have a file "count.txt" where I write the number of elements (and it works), "1t.txt" which represents the title of the first line and "1.txt" which represents the content. I mean just the adding of values value by value. Then when a user hits the title of the Table View I want he reads the content of "*.txt" (obviously easy if I have the index of array file)

For now I use this code:

- (void) loadMainArray
{
    for (int i = 1; i <= max; i++)
    {
    NSString *currentNumber;
    currentNumber = [NSString stringWithFormat:@"%it", i];
    NSLog(@"%i° file read", i);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:currentNumber ofType:@"txt"];
    if ([fileManager fileExistsAtPath:dataFilePath])
    {
        NSString *content = [NSString stringWithContentsOfFile:dataFilePath encoding:NSUTF8StringEncoding error:nil];
        NSArray *parsed = [content componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
        mainArray = (__bridge NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)parsed, kCFPropertyListMutableContainers);
        NSLog(@"%@", parsed);

    }
    else{
    }
    }
}

(sorry if the code is not aligned as well but I'm not able so much in stackoverflow) Anyway, after calling this method in the ViewLoad I get Signal SIGABRT, where I'm wrong?

N.B: max is set as well, I checked in NSLog. mainArray is an NSMutableArray.

4

1 回答 1

5

你想要的是

-(NSArray *)componentsSeparatedByString:(NSString *)separator;

这将在指定的分隔符处划分字符串。所以基本上你这样做: 

NSArray *parsed = [content componentsSeparatedByString:@"\n"];
//assuming you want a newLine as a separator

现在你继续做你的事。玩得开心

编辑:

我不确定你到可变数组的桥在这里是否正确。看来您应该使用 NSData-Object 来捕获调用的返回值

CFPropertyListCreateDeepCopy

如果有任何需要,您可以随时将其转换为 MutableArray。这也解释了您的 SIGABRT 错误。

于 2013-02-18T22:05:12.930 回答