我在 .h
文件中声明了一个 enum
,如下所示:
typedef enum {
item1 = 0,
item2,
item3
} myEnum;
我想在 View Controller 的委托(delegate)方法签名中使用它,如下所示:
@protocol myClassDelegate <NSObject>
- (void)myDelegateMethod:(enum myEnum)type;
@end
我已在此 View Controller 类中包含 .h
文件。
创建上述协议(protocol)时,自动完成功能不建议使用枚举,编译器也会提示。
使用 int
而不是签名中的枚举可以正常工作。但是,我想知道是否有/没有使用枚举的方法,或者我是否做错了什么。
我浏览了很多帖子,但都是正常方法。
编辑:
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "ViewControllerB.h"
typedef enum {
item1 = 0,
item2,
item3
} myEnum;
@interface ViewControllerA : UIViewController <myClassDelegate>
@end
ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
@protocol myClassDelegate <NSObject>
- (void)myDelegateMethod:(enum myEnum)type; // Autocomplete does not suggest the enums
// Also, x-code gives warning: Declaration of 'enum myEnum' will not be visible outside of this functio
@end
@interface ViewControllerB : UITableViewController
@property (nonatomic, strong) id<myClassDelegate> delegate;
@end
最佳答案
您有一个循环 header 依赖项(ViewControllerA.h
导入 ViewControllerB.h
,反之亦然)。
将 enum
声明移至公共(public)头文件中,并在需要的地方导入:
CommonTypes.h:
typedef enum {
item1,
item2,
item3
} MyEnum;
ViewControllerA.h:
#import <UIKit/UIKit.h>
#import "ViewControllerB.h"
@interface ViewControllerA : UIViewController <myClassDelegate>
// I assume there is a reference to ViewControllerB here somewhere?
@end
ViewControllerB.h:
#import <UIKit/UIKit.h>
#import "CommonTypes.h"
@protocol myClassDelegate <NSObject>
- (void)myDelegateMethod:(MyEnum)type;
@end
@interface ViewControllerB : UITableViewController
@property (nonatomic, strong) id<myClassDelegate> delegate;
@end
关于ios - 委托(delegate)方法声明中的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27355041/