简体   繁体   English

当在同一个类中定义方法时,为什么我会使用未声明的标识符'downloadDataFromURL'?

[英]Why do I get Use of undeclared identifier 'downloadDataFromURL' when method is defined in same class?

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 h文件看起来像这样

#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 而m文件如下

#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? 为什么我不能使用在同一个类中声明的方法?

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];
    }
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM