iphone nsarray 有问题吗?

发布于 2024-09-02 02:33:48 字数 1423 浏览 2 评论 0原文

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

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(名称)显示了这个,看起来是正确的:

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"
    );

\

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

Okay maybe i just need another set of eyes on this, but I have the following lines of code in one of my view controllers. It takes some data from a file, and populates it into an array using "\n" as a separator. I then use that array to make an NSDictionary, which is used to populate a tableview. It's very simple. However it isnt working. Here's the code:

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);

so in the log, datastring is correct, so it's correctly pulling the data from a file. however for tempArray, i get:

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

for the tempArray count i get:

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

which is the correct number of strings in the array

So i'm stumped. I have the EXACT same few lines of code in a different view controller and it works perfectly. Whats crazier is the last NSLog, that shows the final NSDictionary (names) displays this, which looks correct:

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"
    );

\

am i missing something really obvious??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野生奥特曼 2024-09-09 02:33:48

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

There's no format specifier in the NSLog statement that prints out the temp array.

月依秋水 2024-09-09 02:33:48

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

它应该看起来像这样:

NSLog([NSString stringwithformat:@"temp array: %@",tempArray]);

you don't have a variable in the string that logs tempArray.

it should look like this:

NSLog([NSString stringwithformat:@"temp array: %@",tempArray]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文