Objective c 为什么要使用未声明的标识符';从URL下载数据';方法何时在同一类中定义?
我已经写了一个方法,我想从同一个类中的另一个方法中重用它,但是我收到了这个错误消息,我不明白为什么,当它在同一个类中声明时,怎么能不声明呢 h文件如下所示Objective c 为什么要使用未声明的标识符';从URL下载数据';方法何时在同一类中定义?,objective-c,Objective C,我已经写了一个方法,我想从同一个类中的另一个方法中重用它,但是我收到了这个错误消息,我不明白为什么,当它在同一个类中声明时,怎么能不声明呢 h文件如下所示 #import <Foundation/Foundation.h> #import "AppDelegate.h" #import "NWTillHelper.h" @interface JoshuaWebServices : NSObject + (void)downloadDataFromURL:(NSURL *)url
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
#import "NWTillHelper.h"
@interface JoshuaWebServices : NSObject
+ (void)downloadDataFromURL:(NSURL *)url withCompletionHandler:(void (^)(NSData *))completionHandler;
- (void)downloadCollections;
@end
为什么我不能使用在同一类中声明的方法?您的方法需要一个接收器。不同于只能自己调用的函数。方法必须由类或类的实例调用。在您的情况下,应该使用类,因为它是类方法 改变
[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];
}
}];
将来
[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];
}
}];
[downloadDataFromURL:url with CompletionHandler:^(NSData*数据)调用类方法时出现语法错误。您可以在此处了解更多信息:好的,这只会给我留下一个错误,当我将委托设置为self时,我会得到以下结果:不兼容的指针类型将“class”发送到“id\u Nullable”类型的参数,将委托设置为nil会清除错误消息,但当然,委托永远不会触发您需要的错误消息要将“@interface JoshuaWebServices:NSObject”更改为“@interface JoshuaWebServices:NSObject”,下面是一个很好的答案,并提供了更多详细信息
[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];
}
}];