1

I have written a method that I want to reuse from another method in the same class but I am getting this error message and I don't understand why, how can it be undeclared when it is declared in the same class?

the h file looks like this

#import <Foundation/Foundation.h>
#import "AppDelegate.h"
#import "NWTillHelper.h"

@interface JoshuaWebServices : NSObject

+ (void)downloadDataFromURL:(NSURL *)url withCompletionHandler:(void (^)(NSData *))completionHandler;

- (void)downloadCollections;

@end

And the m file as follows

#import "JoshuaWebServices.h"

@implementation JoshuaWebServices

@synthesize xmlParser;

+ (void)downloadDataFromURL:(NSURL *)url withCompletionHandler:(void (^)(NSData *))completionHandler {
    if([NWTillHelper isDebug] == 1) {
        NSLog(@"%s entered", __PRETTY_FUNCTION__);
    }
// Lots of irrelevant code here
}

- (void)downloadCollections {
    // Prepare the URL that we'll get the neighbour countries from.
    NSString *URLString = [NSString stringWithFormat:@"https://url.is.not.here"];

    NSURL *url = [NSURL URLWithString:URLString];

    // Download the data.
    [downloadDataFromURL:url withCompletionHandler:^(NSData *data) {
        // Make sure that there is data.
        if (data != nil) {
            self.xmlParser = [[NSXMLParser alloc] initWithData:data];
            self.xmlParser.delegate = self;

            // Initialize the mutable string that we'll use during parsing.
            self.foundValue = [[NSMutableString alloc] init];

            // Start parsing.
            [self.xmlParser parse];
        }
    }];
}

Why can I not use the method declared in same class?

Matt Douhan
  • 2,053
  • 1
  • 21
  • 40
  • [downloadDataFromURL:url withCompletionHandler:^(NSData *data) is syntax error in invoking the class method. You may read more about this here: https://xcodenoobies.blogspot.my/2017/07/how-to-basic-of-xcode-methods-functions.html – GeneCode Dec 08 '17 at 03:45

1 Answers1

3

Your method needs a receiver. Unlike functions that can just be called on there own. Methods must be called by something, either the class, or an instance of a class. In your case you should use a class because it's a class method.

Change

[downloadDataFromURL:url withCompletionHandler:^(NSData *data) {
    // Make sure that there is data.
    if (data != nil) {
        self.xmlParser = [[NSXMLParser alloc] initWithData:data];
        self.xmlParser.delegate = self;

        // Initialize the mutable string that we'll use during parsing.
        self.foundValue = [[NSMutableString alloc] init];

        // Start parsing.
        [self.xmlParser parse];
    }
}];

to be

[JoshuaWebServices  downloadDataFromURL:url withCompletionHandler:^(NSData *data) {
    // Make sure that there is data.
    if (data != nil) {
        self.xmlParser = [[NSXMLParser alloc] initWithData:data];
        self.xmlParser.delegate = self;

        // Initialize the mutable string that we'll use during parsing.
        self.foundValue = [[NSMutableString alloc] init];

        // Start parsing.
        [self.xmlParser parse];
    }
}];
TMin
  • 2,280
  • 1
  • 25
  • 34
  • ok this leaves me with only one error, when I set the delegate to self I get this: Incompatible pointer types sending 'Class' to parameter of type 'id _Nullable' – Matt Douhan Dec 08 '17 at 04:10
  • setting delegate to nil gets rid of the error message but of course then the delegate never fires – Matt Douhan Dec 08 '17 at 04:31
  • You need to change '@interface JoshuaWebServices : NSObject' to be '@interface JoshuaWebServices : NSObject ' here is a great answer with more details https://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – TMin Dec 08 '17 at 16:34