通过委托调用时无法识别协议方法

时间:2011-07-12 15:33:58

标签: objective-c ios delegates protocols

我的问题是,当我通过委托调用协议方法dataLoading时,它只是无法识别它 - 给出expected identifier错误。

这是协议/接口文件:

#import <Foundation/Foundation.h>

@class LoaderView;

@protocol DataLoaderProtocol <NSObject>

@required
- (void) dataLoading;
- (void) doneLoading;

@end

@interface DataLoader : NSObject {

}

@property (retain) id <DataLoaderProtocol> delegate;
@property (retain, nonatomic) LoaderView *loader;

- (id) initWithDelegate: (id <DataLoaderProtocol>) delegate;
- (void) start;

@end

这是实施文件:

#import "DataLoader.h"
#import "LoaderView.h"


@implementation DataLoader

@synthesize delegate = _delegate;
@synthesize loader = _loader;

- (id) initWithDelegate: (id <DataLoaderProtocol>) delegate
{
    self.delegate = delegate;

    return self;
}

- (void) start
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
                                        initWithTarget:self.delegate
                                        selector:@selector([self.delegate dataLoading]) 
                                        object:nil];
    [queue addOperation:operation]; 
    [operation release];
}

@end

错误在此行:selector:@selector([self.delegate dataLoading])

我确信这对我来说是一个愚蠢的错误,但我不明白为什么它没有认识到这种方法,因为代表与协议捆绑在一起......

3 个答案:

答案 0 :(得分:4)

您编写selector:@selector([self.delegate dataLoading])的方式错误,请尝试使用:selector:@selector(dataLoading)

答案 1 :(得分:1)

当您致电self时,我不知道是否已定义initWithDelegate。这可能会搞砸下游的事情......

尝试:

- (id) initWithDelegate: (id <DataLoaderProtocol>) delegate {
    self = [super init];
    if(self) {
        self.delegate = delegate
    }
    return self;
}

答案 2 :(得分:1)

您正在传递选择器(即SEL类型),因此您需要写一下:

NSInvocationOperation *operation = 
    [[NSInvocationOperation alloc] 
        initWithTarget:self.delegate
              selector:@selector(dataLoading) // the name of the selector here 
                object:nil];