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];
}
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
@propertyvariants, you could use this:and you should replace
NSMutableDictionaryin ViewController2 withNSDictionaryandmutableCopywithcopy, 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
svcyou 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.