I’m working through the NSXMLParser sample code and I have a couple of questions about how delegates and properties are set, particularly in the AppDelegate.
In the interface, the AppDelegate is declared to follow the NSXMLParserDelegate protocol, but it doesn’t seem to implement any of the protocol methods or to set itself as the delegate at any point. These seem to be in the ParseOperation class. Is this just a typo?
@interface SeismicXMLAppDelegate : NSObject <UIApplicationDelegate, NSXMLParserDelegate> {
UIWindow *window;
UINavigationController *navigationController;
RootViewController *rootViewController;
@private
// for downloading the xml data
NSURLConnection *earthquakeFeedConnection;
NSMutableData *earthquakeData;
NSOperationQueue *parseQueue;
}
The interface also declares some private properties. These are defined again in an interface extension in the .m file.
@interface SeismicXMLAppDelegate ()
@property (nonatomic, retain) NSURLConnection *earthquakeFeedConnection;
@property (nonatomic, retain) NSMutableData *earthquakeData; // the data returned from the NSURLConnection
@property (nonatomic, retain) NSOperationQueue *parseQueue; // the queue that manages our NSOperation for parsing earthquake data
- (void)addEarthquakesToList:(NSArray *)earthquakes;
- (void)handleError:(NSError *)error;
@end
OK, I think I understand that this means other classes can’t access these properties, and that seems like a really good thing in this case. Here’s the mystery – In the implementation of –applicationDidFinishLaunching: the two properties are defined using different notations.
self.earthquakeFeedConnection =
[[[NSURLConnection alloc] initWithRequest:earthquakeURLRequest delegate:self] autorelease];
vs
parseQueue = [NSOperationQueue new];
Why is one assigned using self.propertyName = and the other with propertyName = ? I know that both parseQueue and earthquakeFeedConnection end up with retain counts of 1, with the difference that earthquakeFeedConnection is part of the autorelease pool and will be released automatically whereas we’ll have to release parseQueue later because of the use of +new and not calling autorelease.
The memory considerations don’t explain the use of self. Is there another difference?
allocandnewreturn objects which are retained once.inithave no effect on memory-management andautoreleasewill release that object later once.if you write
self.myProperty = ...then thesynthesizedsetter is called which behaves like you defined in the according propertynonatomic, retain. Thenonatomicmeans that the getter and setter are not threadsafe (but fast).retainmeans in this case that the setter will release the old object and will retain the new object. if you wroteassigninstead ofretainthe setter would just assign the pointer and wouldn’t call release or retain on the affected objects.The goal in your example is to create two objects which are retained once.
Example1:
Exmaple 2:
parseQueue = [NSOperationQueue new];So in the end both cases result in the same. you could also write
The setter-solution looks more complicated but there are some side-effects. If you notice later that you need special behaviour inside either the getter or setter (for instance: triggering some other methods or do some value-checking: NSString which only should contains eMail-Addresses) then you only have to overwrite the getter or setter. If you dont use
self.myProp = ...then you have to search for usage and have to change that code-snippet as well.The delegate-stuff: yes, you are right: normally you have to (or should) list alle implemented protocols in the interface-definition but the
NSURLConnectionDelegateis an exception. I dont know why (I dont find the implementation-point inNSObject-class-reference) but everyNSObjectimplements that delegate already. So as a result you dont have to mention that your class is implementing that delegate.