从不兼容的指针类型传递'obj_setProperty'的参数4

McK*_*ssy 0 xcode delegates objective-c

我在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
Run Code Online (Sandbox Code Playgroud)

然后在这一行

@synthesize serverDateTimeDelegate;
Run Code Online (Sandbox Code Playgroud)

在ApplicationDelegate.m中我收到错误"从不兼容的指针类型传递'obj_setProperty'的参数4".我做了一些研究,发现'retain'只适用于类类型,这是公平的.如果我实际上删除了行中的"保留"

@property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate;
Run Code Online (Sandbox Code Playgroud)

它没有投诉就编译.但是,我认为,这是错误的做法.当然我的'id' 类类型,当然它应该保留在setter中.顺便说一句,这是我的HeaderPanelViewController的声明,它实现了上述协议:

 @interface HeaderPanelViewController : UIViewController<ServerDateTimeUpdating> {

 }

...
@end
Run Code Online (Sandbox Code Playgroud)

另外,如果我真的删除保留后,我得到一个问题走下赛场时,我实际调用的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;
Run Code Online (Sandbox Code Playgroud)

最后一行导致XCode错误消息"从不兼容的指针类型传递'setServerDateTimeDelegate'的参数1".

Dav*_*ong 6

你的问题是财产声明:

@property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate;
Run Code Online (Sandbox Code Playgroud)

如果你命令 - 双击"id",你会看到它被定义为:

typedef struct objc_object {
  Class isa;
} *id;
Run Code Online (Sandbox Code Playgroud)

换句话说,id已经一个对象引用.因此,*之前的权利serverDateTimeDelegate是不必要的和错误的.拥有它意味着指向对象引用的指针,当你真的只想要一个对象引用时.


Jac*_*kin 6

你的问题在这里:

@property (nonatomic, retain) id<ServerDateTimeUpdating> *serverDateTimeDelegate;
Run Code Online (Sandbox Code Playgroud)

id已经是一个指针类型,因此您声明serverDateTimeDelegate为指针(*)有效地使该属性成为指针的指针.

摆脱它*,一切都应该工作正常.