【问题标题】:Read from txt list file and set many objects in ios从txt列表文件中读取并在ios中设置许多对象
【发布时间】:2012-08-04 14:30:05
【问题描述】:

我,我正在编写一个必须读取 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 ?

【问题讨论】:

  • 抱歉...已修复!谢谢;)

标签: iphone ios xcode properties


【解决方案1】:

【讨论】:

  • 非常感谢!这种方法并不陌生......但是,我的问题是在每个类型的数组或对象数组中设置相同“类型”(如nCh​​apter)的每个组件......
【解决方案2】:

只有当你阅读NSString's class reference:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多