如何将 NSMutableDictionary 传递给另一个视图控制器

发布于 2024-10-31 16:55:34 字数 1224 浏览 3 评论 0原文

我有两个 ViewController,

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

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

方法 1

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

i have two ViewControllers

i need to pass a NSMutableDictionary to the second one, but whats the right way of doing that?

the NSMutableDictionary is modified in First view and passed to second, where it is not modified

METHOD 1

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

}

METHOD 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];   
}

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

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

发布评论

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

评论(1

以为你会在 2024-11-07 16:55:34

两者都不。在方法 1 中,您泄漏了 2 个 NSMutableDictionary 实例,在方法 2 中,您泄漏了其中之一。
而且方法1中的代码根本没有任何意义。你会写x = 1; x = 2; 如果您希望 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]; 

并且您应该将 ViewController2 中的 NSMutableDictionary 替换为 NSDictionarymutableCopy使用 copy ,您不想修改它,不需要首先使其可变。

如果您使用 copy 属性,则无需首先使用 (mutable)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) 一次(第二个 setter 实际上释放第一个对象,但也只释放一次)。因此,两者都将永远留在记忆中。

如果没有泄漏:

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

并且您将在svc中释放(retain - 1),则该对象不再保留并且将被释放。

Neither. In method 1 you are leaking 2 NSMutableDictionary instances, and in method 2 you are leaking one of them.
And the code in method 1 makes no sense at all. Would you write x = 1; x = 2; if you want x to have the value 2? I doubt it, so why do it with objects?

With both @property variants, you could use this:

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

and you should replace NSMutableDictionary in ViewController2 with NSDictionary and mutableCopy with copy, you don't want to modify it, no need to make it mutable in the first place..

and if you use the copy property there is no need to use (mutable)Copy first. You are copying twice.
It's unnecessary but as long as you use proper memory management it doesn't hurt.


Edit: The leaks:

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

but in svc you release (retain - 1) only one time (the second setter actually releases the first object, but only one time either). And so both will hang in memory forever.

Without leaks:

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

and you will release (retain - 1) in svc, the object is no longer retained and it will be deallocated.

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