【问题标题】:How to pass NSMutableDictionary to another View Controller如何将 NSMutableDictionary 传递给另一个视图控制器
【发布时间】:2011-04-10 13:45:04
【问题描述】:

我有两个 ViewControllers

我需要将 NSMutableDictionary 传递给第二个,但正确的做法是什么?

NSMutableDictionary 在第一个视图中被修改并传递到第二个视图,它没有被修改

方法一

- First
@property (nonatomic, retain) NSMutableDictionary * results1;

- Second
@property (nonatomic, retain) NSMutableDictionary * results2;

in First IBAction when iam going to push the second

{
SecondViewController * svc = [[SecondViewController alloc] init];
//svc.results2 is not initialized in Second 
svc.2results = [[NSMutableDictionary alloc] init];
svc.2results = [self.results1 mutableCopy];
//in second view did unload i call [results2 release];

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

}

方法2

- First
@property (nonatomic, retain) NSMutableDictionary * results1;

- Second with COPY
@property (nonatomic, copy) NSMutableDictionary * results2;

{
SecondViewController * svc = [[SecondViewController alloc] init];
//svc.results2 is not initialized in Second 
svc.results2 = [self.results1 mutableCopy];
//in second view did unload i call [results2 release];  

[self.navigationController pushViewController:svc animated:YES];
[svc release];   
}

【问题讨论】:

    标签: objective-c xcode nsmutabledictionary


    【解决方案1】:

    两者都没有。在方法 1 中,您泄漏了 2 个 NSMutableDictionary 实例,在方法 2 中,您正在泄漏其中一个。
    方法1中的代码根本没有意义。如果您希望 x 的值为 2,您会写 x = 1; x = 2; 吗?我对此表示怀疑,那为什么要用对象呢?

    对于两个@property 变体,您可以使用这个:

    SecondViewController * svc = [[SecondViewController alloc] init];
    //svc.results2 is not initialized in Second 
    svc.results2 = [[self.results1 mutableCopy] autorelease];
    //in second view did unload i call [results2 release];
    
    [self.navigationController pushViewController:svc animated:YES];
    [svc release]; 
    

    你应该用NSDictionary替换ViewController2中的NSMutableDictionary,用mutableCopy替换copy,你不想修改它,没必要一开始就让它可变..

    如果您使用复制属性,则无需先使用(可变)复制。您正在复制两次。
    这是不必要的,但只要您使用适当的内存管理,它就不会受到伤害。


    编辑:泄漏:

    svc.2results = [[NSMutableDictionary alloc] init];
       ^ retain +1                       ^^^^^ retain + 1      = retain + 2
    svc.2results = [self.results1 mutableCopy];
       ^ retain +1                ^^^^^^^^^^^ retain + 1       = retain + 2
    

    但是在svc 中,您只释放 (retain - 1) 一次(第二个设置器实际上释放了第一个对象,但也只有一次)。因此,两者都将永远留在记忆中。

    无泄漏:

    svc.2results = [[self.results1 mutableCopy] autorelease];
       ^ retain +1                 ^^^^^^^^^ +1 ^^^^^^^^^^^ -1 later     = retain + 1
    

    你会在svc释放(retain - 1),对象不再保留,会被释放。

    【讨论】:

    • 好的,所以我制作了 result2 NSDictionary 并复制,以及 svc.results2 = [self.results1 autorelease]... 但是在第二个 viewDidUnload 我有 [results2 release] 时我需要让它自动释放吗]?为什么方法 1 和 2 泄漏内存
    • svc.results2 = [self.results1 autorelease] 是错误的。完全错误。仅当您增加保留计数(即,如果您使用副本)时,您才必须自动释放它。内存泄漏是因为 setter(svc.results2 = invokes the setter)正在保留/复制对象本身。
    猜你喜欢
    • 2012-11-13
    • 1970-01-01
    • 2021-12-20
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    相关资源
    最近更新 更多