【问题标题】:Passing argument 4 of 'obj_setProperty' from incompatible pointer type从不兼容的指针类型传递“obj_setProperty”的参数 4
【发布时间】: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


    【解决方案1】:

    你的问题是属性声明:

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

    如果你命令双击“id”,你会看到它定义为:

    typedef struct objc_object {
      Class isa;
    } *id;
    

    换句话说,id 已经是一个对象引用。因此,serverDateTimeDelegate 之前的* 是不必要且错误的。将它放在那里意味着指向对象引用的指针,而实际上您只是想要一个对象引用。

    【讨论】:

      【解决方案2】:

      你的问题在这里:

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

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

      摆脱*,一切都会正常。

      【讨论】:

      • 非常感谢!这确实解决了我的问题。
      猜你喜欢
      • 2016-01-27
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2016-03-28
      相关资源
      最近更新 更多