2011-07-12 104 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];