6

My header file is defined like this:

#import <Foundation/Foundation.h>

@interface Warning: NSObject  { 

In my .m file I do:

NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];

This compiles and works just fine under 3.0.

If I try to compile with 4.0 I get this error:

does not implement the 'NSXMLParserDelegate' protocol

If I add:

@interface Warning: NSObject <NSXMLParserDelegate>  { 

It compiles fine with 4.0, but when I try to compile with 3.0 I get:

error: cannot find protocol declaration for 'NSXMLParserDelegate'

The Foundation framework is found correctly. Adding NSXMLParser.h doesn't help.

Any help would be appreciated.

Thanks

stoutyhk
  • 243
  • 4
  • 10

2 Answers2

4

According to the library reference documentation, NSXMLParser doesn't require a strict NSXMLParserDelegate implementation :

- (void)setDelegate:(id)delegate
- (id)delegate

NSXMLParser checks needed delegate methods availability on the fly.

If NSXMLParser was requiring a NSXMLParserDelegate full implementation, the accessors would be :

- (void)setDelegate:(id<NSXMLParserDelegate>)delegate
- (id<NSXMLParserDelegate>)delegate

I guess that's why there is no NSXMLParserDelegate protocol actually defined in the framework.

So, that interface seems correct to me :

#import <Foundation/Foundation.h>

@interface Warning: NSObject  { 

All you have to do is implement the delegate methods you need for.

Edit :

You could try using preprocessor macros to activate or deactivate the protocol usage declaration :

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
@interface Warning: NSObject <NSXMLParserDelegate>
#else
@interface Warning: NSObject
#endif
{
   // interface definition ...

I didn't try this with 4.0, but it worked on another example between 3.1 and 3.2

  • Thanks I've implemented the delegate methods I need: - (void)parser:(NSXMLParser *)parser foundCharacters: - (void)parser:(NSXMLParser *)parser didStartElement: - (void)parser:(NSXMLParser *)parser didEndElement: – stoutyhk Jun 04 '10 at 11:24
  • But the compiler still complains under 4.0 – stoutyhk Jun 04 '10 at 17:40
  • ah got it. Thanks. Still got an issue though. My min version is zero: minimum version:0.000000 I need to find where it's set. Thanks a lot. – stoutyhk Jun 05 '10 at 15:45
  • Please, don't reply if you don't know the specific of the problem. Defines work during compile time only. The author needs to differentiate this during run-time and make compiler happy too. – Aleks N. Jun 07 '10 at 15:50
  • @stoutyhk, ["iOS Deployment Target" in "Build Settings" corresponds to `__IPHONE_OS_VERSION_MIN_REQUIRED`](http://stackoverflow.com/questions/6617832/run-time-vs-compile-time-iphone-version-check/6625663#6625663). – ma11hew28 Jul 08 '11 at 15:45
2

I was able to fix this with:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
@protocol NSXMLParserDelegate
@end
#endif
@interface Warning: NSObject <NSXMLParserDelegate>

Just be sure to implement the appropriate methods in your implementation.

benvolioT
  • 4,507
  • 2
  • 36
  • 30