2011-07-12 106 views
1

我的问题是当我通过委托调用协议方法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])

我敢肯定,这是我的一个愚蠢的错误,但我不明白为什么它不认可这种方法,因为代表与协议绑定在一起...

回答

4

您写selector:@selector([self.delegate dataLoading])的方式是错误的尝试使用:selector:@selector(dataLoading)代替。

+0

卫生署!是的,因为我已经指定了目标!多么愚蠢的错误。 – xil3

1

我不知道self是否在您致电initWithDelegate时定义。这可能是下游搞乱的东西......

尝试:

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

没错,我们也不需要直接分配吗? '委托= [aDelegate保留];' – 2011-07-12 17:47:50

1

你传递一个选择器(即SEL型),因此,你需要写:

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