Question

I want to add an argument to a delegate methode (From NSXMLParserDelegate)

this is the method so far :

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    // save the characters for the current item...
    if ([string   isEqual: @"off"]) {
         myObject.isON = NO; //doesn't know what is myObject
    }

What I want :

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string:(MyObject*)anObject{
    // save the characters for the current item...
    if ([string   isEqual: @"off"]) {
         anObject.isON = NO;
    }

Thank you

Was it helpful?

Solution

First you need to subclass your NSXMLParser, add new delegate property call it subclassDelegate or something similar, so you can differentiate between the super class's delegate. In init be the delegate of your superclass self.delegate = self;

respond to the delegate methods and forward the methods that you don't want to override to the self.subclassDelegate respond to the method that you want to override and override it in your subclass protocol.

Here is the example:

    @protocol  MyXMLParserDelegate;
@interface MyXMLParser : NSXMLParser<NSXMLParserDelegate>
@property (weak) id<MyXMLParserDelegate> subclassDelegate;
@end

@protocol  MyXMLParserDelegate <NSObject>
- (void)parserDidStartDocument:(NSXMLParser *)parser;
// this is the method that you override
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string withObject:(id)object;
@end

Then in .m

@implementation MyXMLParser

- (id)init
{
    self = [super init];
    if(self) {
        self.delegate = self;
    }
    return self;
}


#pragma mark - repspond to NSXMLParser delegate

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [self.subclassDelegate parser:parser foundCharacters:string withObject:yourObject];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top