OGeek|极客世界-中国程序员成长平台

标题: iPhone dev、NSDictionary如何保留完整的Dict? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:02
标题: iPhone dev、NSDictionary如何保留完整的Dict?

在嵌套的 NSDictionary 中保留数据时遇到问题。还是 NSMutableDictionary 的某些东西可以使这项工作?看一下,我会尽量解释清楚。

我的 .h 文件如下所示:

@interface MyViewController : UIViewController 
<UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary *fullData;
    IBOutlet UITableView *tableView;
}

@property (nonatomic, retain) NSDictionary *fullData;
@property (nonatomic, retain) UITableView *tableView;

@end

我在 viewDidLoad 中设置了我的 inits

- (void)viewDidLoad {
    ...
    fullData = [NSDictionary dictionaryWithContentsOfURL:url];
    [fullData retain];
    [super viewDidLoad];
}

当我尝试将它插入 UITableViewCells 以及我需要做的任何事情时,这工作正常,即使我在此处执行打印 fullData 的 NSLog,也会显示所有数据。 像这样:

2010-11-24 14:49:53.334 MyApp[25543:40b] {
    items =     (
                {
            id = 5;
            localId = 1;
            name = "A name1";
        },
                {
            id = 8;
            localId = 3;
            name = "A name2";
        },
                {
            id = 9;
            localId = 4;
            name = "A name3";
        },
                {
            id = 10;
            localId = 5;
            name = "A name4";
        },
                {
            id = 11;
            localId = 6;
            name = "A name5";
        }
    );
    results = 5;
}

虽然这很有效,但我想将 fullData 保留在我的其他事件中,例如 didSelectRowAtIndexPath。首先,我必须保留 ,如果我这样做了,只会保留第一级数据。 dict 项只会指向一些不存在的内存。

所以我试试:

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
    NSLog(@"Data: %@", fullData);
}

这有时会返回:

2010-11-24 14:44:28.288 MyApp[25493:40b] Data: {
    items =     (
        "<_NSIndexPathUniqueTreeNode: 0x9d2a820>",
        "<CALayer: 0x9d27980>",
        "<CALayer: 0x9d2bc30>",
        "<CALayer: 0x9d29830>",
        "<CALayer: 0x9d299e0>"
    );
    results = 5;
}

似乎保留了一些值,但无法访问项目内的数据。将数据存储到本地文件然后再次访问它对我来说更好,还是应该可以保留完整的字典?

我尝试添加 [[fullData objectForKey"items"] retain];

我对此很陌生,因此我需要帮助以使我的代码也遵循最佳实践。我尝试了很多解决方案,也看过苹果和其他地方的几部电影。我就是无法解决。这可能很简单,但我不知道在哪里看。

谢谢。


对不起,我自己解决了这个问题 我没有包含这个函数:

- (UITableViewCell *)tableViewUITableView *)tableView 
         cellForRowAtIndexPathNSIndexPath *)indexPath
{
    NSLog(@"Retain count: %i", [fullData retainCount]);
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier"cell"];

    // create a cell
    if( cell == nil )
    {
        cell = [[UITableViewCell alloc] 
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier"cell"];
    }

    // fill it with content
    NSArray *current = [[fullData objectForKey"items"] objectAtIndex:indexPath.row];
    NSString *rowLabel = [NSString stringWithFormat"%@, %@",[current valueForKey"localId"], [current valueForKey"name"]];
    cell.textLabel.text = rowLabel;
    [current release];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // return it
    return cell;
}

问题是我释放表中每一行的当前变量。这应该是因为变量 current 中的实例不是副本,而是真正的引用。

还是谢谢。



Best Answer-推荐答案


fullData = [NSDictionary dictionaryWithContentsOfURL:url];

自动释放。你不应该保留它。使用这个:

self.fullData = [[NSDictionary alloc] initWithContentsOfURL:url];

而且我认为您现在不需要出于您的目的保留它。只有在 MyViewController 发布后才需要访问它。

你要发布 UITableView 吗?它可能会通过并释放您的 NSDictionary 中的单元格。 没有更多代码,我不知道。

关于iPhone dev、NSDictionary如何保留完整的Dict?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4267653/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4