3

TurbineXMLParser.h

#import <Foundation/Foundation.h>

@interface TurbineXMLParser : NSObject <NSXMLParserDelegate> {
...

TurbineXMLParser.m

#import "TurbineXMLParser.h"

I have just added a new class to my current project that I previously tested in a single file. When I try and build the project I get the error: error: cannot find protocol declaration for 'NSXMLParserDelegate'

I did a bit of searching and tried adding the following ...

TurbineXMLParser.h

#import <Foundation/Foundation.h>

@protocol NSXMLParserDelegate;

@interface TurbineXMLParser : NSObject <NSXMLParserDelegate> {
...

but still get the warning: warning: no definition of protocol 'NSXMLParserDelegate' is found

any help would be much appreciated

.
.
.

EDIT_002:

Removing <NSXMLParserDelegate> from the @interface did work, but I am curious as to why, am I getting mixed up & muddled? I was under the impression that the delegate object must adopt the NSXMLParserDelegate protocol i.e. adding <NSXMLParserDelegate> after the superclass.

I have two instances where this is working differently, the first is a project in a single command line file where If I don't add <NSXMLParserDelegate> is warns that:

class 'TestXMLParser' does not implement the 'NSXMLParserDelegate' protocol

The second instance is where I have setup multiple *.h and *.m files (one of the classes being MyXMLParser.h, MyXMLParser.m) when I try to build the project with <NSXMLParserDelegate> I get this error:

error: cannot find protocol declaration for 'NSXMLParserDelegate'

Remove <NSXMLParserDelegate> and it all works fine, no errors, no warning...

gary

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294

4 Answers4

6

Forward-declaring NSXMLParserDelegate isn't likely to help, it needs to be actually imported if you're conforming to it.

The original error is what you'd get if you haven't linked the Foundation framework into your project.

Get-info your target in XCode. In the General tab, make sure Foundation.framework is in your list of linked libraries.

Jeffrey Harris
  • 3,480
  • 25
  • 30
  • The @protocol was just me trying things out, I now realise that its wrong. The Foundation framework not being found is what I thought, I probably set up a loop or missed it out completely, many thanks for the heads-up. – fuzzygoat Apr 06 '10 at 11:17
  • That line simply forward-declares the protocol, it doesn't define it as being empty. But of course the protocol needs to be defined at the point that it is used. – Mike Weller Apr 15 '10 at 12:52
  • Ah, I'd totally forgotten you could forward-declare a protocol, you're right. Editing to remove the question about that line. – Jeffrey Harris Apr 15 '10 at 19:47
5

You don't need to define your object to be an NSXMLParserDelegate

Just make sure you do this:

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];

And implement the methods in that object.

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
Felix Khazin
  • 1,479
  • 2
  • 10
  • 16
  • I have done exactly like what u said but in the 3rd delegate method i.e. parser: foundCharacters: I get null in the String whereas there is some value. – Ank Sep 27 '10 at 05:53
  • This doesn't compile for me with a Base SDK of 4.1. The compile fails at setDelegate with "Class 'MyClass' does not implement the 'NSXMLParserDelegate' protocol". I can't find a way to make this build on both 3.2 and 4.1. Other posts suggest using a minimum OS version, but I want to be able to run on older handset OSes. http://stackoverflow.com/questions/2966475/nsxmlparserdelegate-compile-problem-iphone-sdk-30-vs-4-0 – Dan J Oct 22 '10 at 00:51
  • Ah, I've just realized that the message I mention is a warning, not an error. I have the Xcode "treat warnings as errors" build option set, which explains why I am seeing the error. – Dan J Oct 22 '10 at 01:23
4

Although NSXMLParser was introduced from iOS2, The delegate hasn't been introduced until iOS4. Just remove the protocol declaration from your class, that worked for me when compiling against 3.2.

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSXMLParserDelegate_Protocol/Reference/Reference.html

matiaslanzi
  • 103
  • 8
2

You do need to say it's an NSXMLParserDelegate in SDK4.0. But when I add to the interface definition, it no longer compiles under 3.0, just 4.0.

Under 3.0 it says that it cannot find the protocol def for NSXMLParserDelegate. I've included:

#import <Foundation/NSXMLParser.h>
Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
stoutyhk
  • 243
  • 4
  • 10