In short, the <NSXMLParserDelegate>
in your interface declaration is not required, it's there for error checking at compile time. Calling the setDelegate:
method is required if you want to receive delegate messages.
The <delegateStuff>
you refer to are protocol declarations. A Protocol in Objective-C is like an Interface in Java: it defines a list of messages (methods) that the class responds to. Unlike Java, however, Objective-C is dynamically typed, meaning any message can be sent to any object and you can only be sure if it will respond to it at run time.
The upshot of this is that if a method like setDelegate:
asks for a parameter of type id
, you can give it any object whatsoever. The code in NSXMLParser can check if it is able to respond to a specific message before sending it. That way you can implement whatever delegate methods you want. (Because Java has more strict type checking, you must implement all the methods in an interface, whether you need them all or not.)
If setDelegate:
instead act for a type id <NSXMLParserDelegate>
it is now saying that it wants an object that implements the NSXMLParserDelegate protocol. This is only checked at compile time to help you catch errors, though. If you use a typecast you can send any object in there and, as long as it responds to the required messages, your program will run fine. (Again, in Java, you could use a typecast to compile, but the typecast itself would throw an exception if the object wasn't of the correct type, even if it had the same methods.)
By the way, are you subclassing NSXMLParser? (I assume so, since you are passing self
to setDelegate:
. That is not the way it is meant to be used! The Cocoa frameworks usually prefer delegates over subclasses. Your delegate class should be a separate class extending NSObject (or something else if you want).