【发布时间】:2011-01-19 08:00:23
【问题描述】:
我在 XCode 中遇到上述编译器错误,我无法弄清楚发生了什么。
#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;
它确实可以毫无怨言地编译。然而,我认为,这是错误的做法。当然,我的 'id' 是 一个类类型,它肯定 应该 保留在 setter 中。顺便说一句,这是实现上述协议的 HeaderPanelViewController 的声明:
@interface HeaderPanelViewController : UIViewController<ServerDateTimeUpdating> {
}
...
@end
另外,如果我真的 确实 删除了保留,那么当我实际调用 setter 将我的 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;
最后一行导致 XCode 错误消息“Passing argument 1 of 'setServerDateTimeDelegate' from in compatible pointer type”。
【问题讨论】:
标签: objective-c xcode delegates