0

我,我正在编写一个必须读取 txt 内容的应用程序。这个 txt 就是这样一个属性文件,其列表格式如下:

1|Chapter 1|30

2|Chapter AA|7

3|Story of the United States|13

........

键用“|”分隔。

我在谷歌上搜索了很多,希望能找到任何“实用的解决方案”,但什么也没有……我如何阅读这些信息并设置许多对象,例如:

for    NSInterger *nChapter = the first element

for    NSString *title = the second element

for    NSInteger *nOfPages = the last element ?
4

2 回答 2

1

NSString- (NSArray *)componentsSeparatedByString:(NSString *)separator可能是你最好的朋友。

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-componentsSeparatedByString_

于 2012-08-07T14:37:03.283 回答
0

仅当您阅读NSString 的类参考时:

NSString *str = [NSString stringWithContentsOfFile:@"file.txt"];
NSArray *rows = [str componentsSeparatedByString:@"\n"];

for (NSString *row in rows)
{
    NSArray *fields = [row componentsSeparatedByString:@"|"];
    NSInteger nChapter = [[fields objectAtIndex:0] intValue];
    NSString *title = [fields objectAtIndex:1];

    // process them in here
}
于 2012-08-07T14:41:34.883 回答