2011-01-18 100 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

相关问题