2011-01-18 65 views
0

我在XCode中收到了上述編譯器錯誤,我無法弄清楚發生了什麼。從不兼容的指針類型傳遞'obj_setProperty'的參數4

#import <UIKit/UIKit.h> 

// #import "HeaderPanelViewController.h" 
#import "HTTPClientCommunicator.h" 
#import "WebSocket.h" 

@class HeaderPanelViewController; 

@protocol ServerDateTimeUpdating 
-(void)serverDateTimeHasBeenUpdatedWithDate:(NSString *) dateString andTime:(NSString *) timeString; 
@end 

@interface SmartWardPTAppDelegate : NSObject <UIApplicationDelegate, WebSocketDelegate> { 

} 

@property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate; 
.... 
@end 

然後在此行

@synthesize serverDateTimeDelegate; 
在ApplicationDelegate.m

我正在錯誤 「傳遞從兼容的指針類型 'obj_setProperty' 的參數4」。我做了一些研究,發現'保留'只適用於班級類型,這很公平。如果我實際上刪除了「保留」行

@property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate; 

它確實編譯沒有投訴。但是,我認爲,這是錯誤的做法。當然我的'身份證'一類的類型,當然它應該被保留在二傳手。順便說一句,這是我HeaderPanelViewController的它實現了上述協議的聲明:

@interface HeaderPanelViewController : UIViewController<ServerDateTimeUpdating> { 

} 

... 
@end 

而且,如果我真的除去保留以後,我的問題走下賽場時,我竟然叫二傳手註冊我的HeaderPanelViewController作爲委託:

// Register this instance as the delegate for ServerDateTimeUpdating 
// Retrieve the ApplicationDelegate... 
ApplicationDelegate *applicationDelegate = (ApplicationDelegate *) [UIApplication sharedApplication].delegate; 
// ...and register this instance 
applicationDelegate.serverDateTimeDelegate = self; 

最後一行導致「傳遞從兼容的指針類型‘setServerDateTimeDelegate’的參數1」的的XCode錯誤消息。

回答

6

你的問題就是財產申報:

@property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate; 

如果命令雙擊 「ID」,你會看到它定義爲:

typedef struct objc_object { 
    Class isa; 
} *id; 

換句話說,id已經有一個對象引用。因此,在serverDateTimeDelegate之前*是不必要的和錯誤的。有了它,意味着一個指向對象引用的指針,當你真的想要一個對象引用。

6

你的問題是在這裏:

@property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate; 

id已經是一個指針類型,所以你聲明serverDateTimeDelegate爲指針(*)能有效地使性的指針的指針。

擺脫*和一切應該正常工作。

+0

非常感謝!這確實解決了我的問題。 – McKrassy 2011-01-19 00:01:45

相關問題