3

好吧,也许我只需要另一双眼睛,但我的一个视图控制器中有以下代码行。它从文件中获取一些数据,并使用“\n”作为分隔符将其填充到数组中。然后我使用该数组创建一个 NSDictionary,用于填充 tableview。这很简单。但是它不起作用。这是代码:

NSString *dataString = [NSString stringWithContentsOfFile:checklistPath encoding: NSUTF8StringEncoding error:NULL];


    if ([dataString hasPrefix:@"\n"]) {
        dataString = [dataString substringFromIndex:1];

    }
    NSArray *tempArray = [dataString componentsSeparatedByString:@"\n"];
    NSLog(@"datastring:%@",dataString);
    NSLog(@"temp array:",tempArray);
    NSLog(@"%i",[tempArray count]);

    NSDictionary *temporaryDictionary = [NSDictionary dictionaryWithObject: tempArray forKey:@"User Generated Checklist"];
    self.names = temporaryDictionary;
            NSLog(@"names:%@",names);

所以在日志中,数据字符串是正确的,所以它正确地从文件中提取数据。但是对于 tempArray,我得到:

2010-05-17 19:15:55.825 MyApp[7309:207] temp array:

对于 tempArray 计数,我得到:

2010-05-17 19:15:55.826 myApp[7309:207] 5

这是数组中正确的字符串数

所以我很难过。我在不同的视图控制器中有完全相同的几行代码,它工作得很好。更疯狂的是最后一个 NSLog,它显示了最终的 NSDictionary (names) 显示了这个,看起来是正确的:

2010-05-17 19:15:55.827 FS Companion[7309:207] names:{
    "User Generated Checklist" =     (
        "System|||ACTION",
        "System|||ACTION",
        "System|||ACTION",
        "System|||ACTION",
        "System|||ACTION"
    );

\

我错过了一些非常明显的东西吗?

4

2 回答 2

2

打印临时数组的 NSLog 语句中没有格式说明符。

于 2010-05-18T01:34:38.253 回答
1

您在记录 tempArray 的字符串中没有变量。

它应该是这样的:

NSLog([NSString stringwithformat:@"temp array: %@",tempArray]);
于 2010-05-18T01:33:30.627 回答