If I have a local pointer to a custom class, how can I store its pointer in a class level NSDictionary variable and use it after its initial creation scope has terminated.
How can I tell the compiler to retain the memory created by new until the class I created it in has gone out of scope?
This is sudo code to represent the scoping of the variables in question.
Class {
NSMutableDictionary* allElements;
SomeFunction {
InnerClosure1 {
DefaultElement* element = [DefaultElement new];
[allElements setObject:element forKey:@:"Foo"];
}
InnerClosure2 {
DefaultElement* element = [allElements objectForKey:@"Foo"];
}
}
The problem is I get this error after trying to put the pointer in the dictionary:
[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to
instance 0x94421d0 2013-01-31 23:35:32.575
Muskoka-Domicile[1371:19d03] * Terminating app due to uncaught
exception ‘NSInvalidArgumentException’, reason: ‘-[__NSDictionaryI
setObject:forKey:]: unrecognized selector sent to instance 0x94421d0’
I suspect that it has something to do with transferring ownership of a custom class?
Edit
Turns out initialized the NSMutableDictionary* to a NSDictionary…
Your local pointer can be lost. What you’re interested in is the chunk of memory (object) you’re pointing at.
When you add any object to NSDictionary, that object is retained by the dictionary. You can leave scope1 and your object will be just fine. In fact, if you are not using arc, after adding your element to the dictionary you would typically call
[element release]to balance out your call tonew.Later, when you pull the object out of the dictionary, you are just updating the temporary pointer (element) with the memory address of your object. In ‘Scope2’ Since you did not create the object, you would not release it when done.
Hope that helps clarify things.
edit/update
I should note, your code is basically correct as is. If you want to put objects into a dictionary, you need an
NSMutableDictionary, so make one of those at the top of your code. Next, you should callsetObject:forKey:notsetValue. That should do the trick for you. You may need to add a cast to(DefaultElement *)to suppress a warning when you pull your element out of the dictionary.