在plist iphone中访问数组中的字符串

时间:2010-10-05 02:01:26

标签: iphone nsstring nsarray plist nsdictionary

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

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

此行在我的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];

}

2 个答案:

答案 0 :(得分:1)

我的猜测是您没有保留aDictionarykeys,或者您在代码中的那一点上出现了另一个内存问题。但是没有剩下的代码就很难说清楚。

答案 1 :(得分:0)

将此行更改为

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

程序在哪里崩溃,是什么行?