如何将 NSMutableDictionary 传递给另一个视图控制器
我有两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两者都不。在方法 1 中,您泄漏了 2 个 NSMutableDictionary 实例,在方法 2 中,您泄漏了其中之一。
而且方法1中的代码根本没有任何意义。你会写
x = 1; x = 2;
如果您希望 x 的值为 2?我对此表示怀疑,那为什么要用对象来做呢?对于这两种
@property
变体,您可以使用:并且您应该将 ViewController2 中的
NSMutableDictionary
替换为NSDictionary
和mutableCopy
使用copy
,您不想修改它,不需要首先使其可变。如果您使用 copy 属性,则无需首先使用 (mutable)Copy 。您复制了两次。
这是不必要的,但只要您使用正确的内存管理,它就不会造成伤害。
编辑:泄漏:
但在 svc 中,您仅释放 (retain - 1) 一次(第二个 setter 实际上释放第一个对象,但也只释放一次)。因此,两者都将永远留在记忆中。
如果没有泄漏:
并且您将在
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:and you should replace
NSMutableDictionary
in ViewController2 withNSDictionary
andmutableCopy
withcopy
, 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:
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:
and you will release (retain - 1) in
svc
, the object is no longer retained and it will be deallocated.