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

发布于 2024-09-26 06:20:44 字数 3128 浏览 0 评论 0原文

我有一个 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];

}

I have a plist with root of type dictionary containing keys of type array, and each array has three items of type string.

I want to access the strings and pass them to a view controller.

This line is successful in my 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.

Any suggestions as to how to get at the strings would be helpful.

Here is my .plist

<dict>
    <key>Derivative</key>
    <array>
        <string>Derivative 1</string>
        <string>front.png</string>
        <string>back.png</string>
    </array>
    <key>Rolle's Theorem</key>
    <array>
        <string>Rolle'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];

}

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

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

发布评论

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

评论(2

ま柒月 2024-10-03 06:20:44

我的猜测是,您没有保留 aDictionarykeys,或者代码中此时出现了另一个内存问题。但如果没有其余的代码,很难判断。

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. But it's difficult to tell without the rest of the code.

時窥 2024-10-03 06:20:44

将此行更改为

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

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

Change this line to

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

Where does the program crash, what line?

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