Monday, 20 September 2010

NSXMLParserDelegate fix for different iPhone iOS versions

This is a short blog providing a better fix than I've yet seen for supporting consistent NSXMLParserDelegate code across different versions of iOS.

The problem occurs because the NSXMLParser class was changed between iOS 3.0 and 4.0. Originally it informally declared some delegate methods and this was turned into a formal delegate protocol: NSXMLParserDelegate.

This means that you'd need to fake the delegate class with:

@protocol NSXMLParserDelegate
@end


if you're compiling for a iOS 3.0 target, but this will generate a duplicate @protocol declaration warning if you then compile for after 4.x. So, really you want the protocol declaration only for versions of iOS below 4.0.

The easiest way to do this is simply to wrap the protocol declaration with:

#ifndef __iphone_4_0
@protocol nsxmlparserdelegate
@end
endif


That's how you test for the current SDK version - in any particular sdk there's a #define for each version up until the current version, so version 4.1 has __IPHONE_

-cheers from julz

No comments: