I am writing a plugin class for a framework.
My interface is defined like so:
@interface Blah : NSObject<SomeDelegate>
{
@private
NSString* _callbackID;
}
I have a private member variable as the framework will call a method in my class passing it an array containing a NSString which is required by another method in a subsequent call. I don’t want it visible outside the class.
In the first method that is called I am doing something like this:
- (void) blah:(NSArray*)blahArray
{
_callbackID = [blahArray objectAtIndex:0];
MyViewController *vc = [[MyViewController alloc] init];
vc.someDelegate = self;
[self.viewController presentModalViewController:vc animated:YES];
[vc release];
}
As a delegate my plugin Blah class will receive a call back from MyViewController where it will need to use this _callbackID.
- Am I handling the private variable correctly, and am I assigning it a value correctly?
- I understand I shouldn’t have to manage the
NSStringfrom theblahArrayas I didn’t create it is this correct? - If the call to blah was asynchronous what would stop the
NSArraybeing cleaned up in object callingblahleaving me with nothing in _callbackID? How would I know this was the case? - If the NSString
_callbackIDis then assigned to an existing object not under the control of the class callingblahwould it have to be copied to ensure the calling class didn’t clean it up?
1. Am I handling the private variable correctly, and am I assigning it a value correctly?
You can make it easier on yourself by always using properties. Add this to your interface (outside the braces):
@property (nonatomic, copy) NSString *_callbackID;And then have the compiler automatically generate get/set methods, by adding this to your implementation:
@sythesize _callbackID;Then you can just write:
self._callbackID = [blahArray objectAtIndex:0];which transparently calls the automatically-generated
set_callbackIDmethod, which will copy the value (due to thecopyhint in the property descriptor).2. I understand I shouldn’t have to manage the NSString from the blahArray as I didn’t create it is this correct?
You are not responsible for releasing it, but if you want to keep it around, you need to either
retainorcopyit. Once you do that, you are responsible for releasing it (see Gilad’s answer for more).3. If the call to blah was asynchronous what would stop the NSArray being cleaned up in object calling blah leaving me with nothing in _callbackID? How would I know this was the case?
By now you have probably realized that without a
retainorcopy, the variable could be released while you’re using it. That’s why you need to make it your own. You would know if the variable was released out from under you because your application would crash with EXC_BAD_ACCESS ;-).4. If the NSString _callbackID is then assigned to an existing object not under the control of the class calling blah would it have to be copied to ensure the calling class didn’t clean it up?
Yes. And if you want to make sure a variable stays around for the duration of your function, you could write
[[myVar retain] autorelease]. Then it will automatically be released at the end of the run loop (or whenever the current autorelease pool is drained).