gpt4 book ai didi

ios - 委托(delegate)方法声明中的枚举

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:47 25 4
gpt4 key购买 nike

我在 .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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com