【问题标题】:Accessing string within an array within a plist iphone访问plist iphone中的数组中的字符串
【发布时间】:2010-10-05 02:01:25
【问题描述】:

我有一个 plist,其根类型为字典,其中包含数组类型的键,每个数组都有三个字符串类型的项。

我想访问字符串并将它们传递给视图控制器。

这一行在我的 cellForRowAtIndexPath 中是成功的

NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);

当我在 didSelectRowAtIndexPath 方法中放入同一行时,我收到 BAD_ACCESS 错误。

任何关于如何获取字符串的建议都会有所帮助。

这是我的.plist

<dict>
    <key>Derivative</key>
    <array>
        <string>Derivative 1</string>
        <string>front.png</string>
        <string>back.png</string>
    </array>
    <key>Rolle&apos;s Theorem</key>
    <array>
        <string>Rolle&apos;s Theorem 1</string>
        <string>front.png</string>
        <string>3bk.png</string>
    </array>
    <key>Chain Rule</key>
    <array>
        <string>Chain Rule</string>
        <string>chainrule1.png</string>
        <string>chainrule1_bk.png</string>
    </array>
</dict>
</plist>

这里有两种方法:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }


    NSUInteger row = [indexPath row];

    cell.backgroundColor = [UIColor purpleColor];
    cell.textLabel.textColor = [UIColor blackColor];

    cell.textLabel.text = [self.keys objectAtIndex:row];

    // Key output to NSLOG accessing elements from the plist
    NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);

    cell.textLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:18.0];
    cell.textLabel.numberOfLines = 2;
    cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCell_BG.png"]] autorelease];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    NSLog(@"Row selected was: %i", row);

    // Key output to NSLOG accessing elements from the plist
    //NSLog(@"Strings from plist: %@", [[self.aDictionary objectForKey:(@"%@",[self.keys objectAtIndex:row])] objectAtIndex:0]);


/*
    NSString *selectedCard = [[aDictionary objectForKey:(@"%@",[keys objectAtIndex:row])] objectAtIndex:0];
    NSLog(@"Showing Flash Card: %@", selectedCard);
*/

    FlashCardViewController *flashVC = [[FlashCardViewController alloc] initWithNibName:@"FlashCardViewController" bundle:nil];

    id aKey = [keys objectAtIndex:row];

    flashVC.aSelectedCard = [[aDictionary objectForKey:aKey] objectAtIndex:0];

    [self.navigationController pushViewController:flashVC animated:YES];
    [flashVC release];

}

【问题讨论】:

  • 您错误地访问了 indexPath 的行变量。看看我的帖子,它会告诉你如何摆脱 EXC_BAD_ACCESS 错误
  • NSUInteger row = [indexPath row];那不应该工作它应该是 NSUInteger row = indexPath.row;这有效,应该消除错误。我只是自己尝试过,我没有收到任何错误

标签: iphone nsstring nsarray plist nsdictionary


【解决方案1】:

我的猜测是你没有保留aDictionarykeys,或者你有另一个内存问题出现在代码中。但是如果没有剩下的代码就很难分辨了。

【讨论】:

  • 在我的头文件中我这样声明变量:
  • @property (nonatomic, 保留) NSDictionary *aDictionary; @property (nonatomic, 保留) NSMutableArray *keys;这足以保留变量吗?
  • 取决于你如何构建它们:aDictionary = [NSDictionary dictionaryWithContentsOfFile:path] 不够
  • 我认为因为我在我的 .h 文件中使用 @property (nonatomic, retain) NSDictionary *aDictionary; 声明它那会让它持续存在。您将如何设置它以使其不会被释放?我真的认为这就是它表现得如此的原因。
  • 你要么做self.aDictionary = [NSDictionary dictionaryWithContentsOfFile:path]要么aDictionary = [[NSDictionary alloc] initWithContentsOfFile:path]
【解决方案2】:

将此行改为

flashVC.aSelectedCard = [[aDictionary objectForKey:[keys objectAtIndex:row]] objectAtIndex:0];

程序在哪里崩溃,哪一行?

【讨论】:

  • 如图所示,程序不会崩溃,但不会从 plist 中拉取字符串。如果我取消注释我打印到 NSLog“来自 plist 的字符串”的第二行,这将使其崩溃。但是在之前的方法中,对 NSLog 的相同调用将正常运行并在控制台中显示字符串。我现在不在我的 Mac 上,但会试试你的电话,看看它是否能实现我想要实现的目标。
  • 在帖子中,您声明访问不正确。在哪一行?
  • 我试过这条线,我仍然像以前一样在这条线上得到 EXC_BAD_ACCESS。 :(
  • 在调用此行之前,使用 NSLog 打印字典的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-07
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
相关资源
最近更新 更多