简体   繁体   English

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

[英]Accessing string within an array within a plist iphone

I have a plist with root of type dictionary containing keys of type array, and each array has three items of type string. 我有一个带有类型字典根的plist,其中包含类型数组的键,每个数组都有三个类型的字符串。

I want to access the strings and pass them to a view controller. 我想访问字符串并将它们传递给视图控制器。

This line is successful in my cellForRowAtIndexPath 这行在我的cellForRowAtIndexPath中成功

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

When I put the same line in my didSelectRowAtIndexPath method I get a BAD_ACCESS error. 当我在didSelectRowAtIndexPath方法中放入同一行时,出现BAD_ACCESS错误。

Any suggestions as to how to get at the strings would be helpful. 关于如何获得琴弦的任何建议都将有所帮助。

Here is my .plist 这是我的.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>

And here are the two methods: 这是两种方法:

 - (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];

}

My guess is that you are not retaining aDictionary or keys , or you have another memory problem that is manifesting at that point in the code. 我的猜测是您没有保留aDictionarykeys ,或者您在代码中此时还存在另一个内存问题。 But it's difficult to tell without the rest of the code. 但是,如果没有其余代码,很难说出来。

Change this line to 将此行更改为

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

Where does the program crash, what line? 程序在哪里崩溃,哪一行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM