代表iOS

时间:2013-11-29 21:46:54

标签: ios objective-c

我试图了解为什么无法从协议中读取该类型。我认为这是因为@interface低于协议但是如果有人可以帮助我找出问题并向我解释为什么那会很棒。

#import <UIKit/UIKit.h>

@protocol ARCHCounterWidgetDelegate <NSObject>

    - (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget;

@end

@interface ARCHCounterWidget : UIView

    @property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate;

@end

2 个答案:

答案 0 :(得分:4)

您必须forward declare课程或协议:

// tell the compiler, that this class exists and is declared later:
@class ARCHCounterWidget;

// use it in this protocol
@protocol ARCHCounterWidgetDelegate <NSObject>
- (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget;
@end

// finally declare it
@interface ARCHCounterWidget : UIView
@property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate;
@end

或:

// tell the compiler, that this protocol exists and is declared later
@protocol ARCHCounterWidgetDelegate;

// now you can use it in your class interface
@interface ARCHCounterWidget : UIView
@property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate;
@end

// and declare it here
@protocol ARCHCounterWidgetDelegate <NSObject>
- (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget;
@end

答案 1 :(得分:2)

编译器尚未知道ARCHCounterWidget,因此无法解析委托方法中的类型。简单的解决方案是:

#import <UIKit/UIKit.h>

// Reference the protocol
@protocol ARCHCounterWidgetDelegate

// Declare your class
@interface ARCHCounterWidget : UIView

    @property (weak, nonatomic) id<ARCHCounterWidgetDelegate> delegate;

@end

// Declare the protocol
@protocol ARCHCounterWidgetDelegate <NSObject>

    - (UIColor *) counterWidget: (ARCHCounterWidget *) counterWidget;

@end

它只是使编译器知道协议已定义为SOMEWHERE并且可以引用