gpt4 book ai didi

objective-c - 将枚举值分配给 Obj-C 中的属性

转载 作者:搜寻专家 更新时间:2023-11-01 07:23:34 25 4
gpt4 key购买 nike

我在 Swift 中有以下代码。我如何将其转换为 Objective-C? (是的,从 Swift 到 Objective-C);)

class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {

enum PopTransitionMode: Int {
case Present, Dismiss
}

var transitionMode: PopTransitionMode = .Present

}

我在 Obj-C 中尝试了以下方法,我不确定如何将 PopTr​​ansitionMode 的值类型分配给我的属性。

// .h
typedef NS_ENUM(NSInteger, PopTransitionMode) {
Present,
Dismiss
};
@interface PopAnimator : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) PopTransitionMode transitionMode;
@end


// .m
@implementation PopAnimator

-(id)init {

if (self = [super init]) {

_transitionMode = ???

}


return self;
}

@end

最佳答案

您可以只使用枚举值。有了更好的命名,像这样枚举:

typedef NS_ENUM(NSInteger, PopTransitionMode) {
PopTransitionModePresent,
PopTransitionModeDismiss
};

像这样初始化:

// if the initializer takes a param
- (id)initWithTransitionMode:(PopTransitionMode)transitionMode {
self = [self init];
if (self) {
_transitionMode = transitionMode;
}
return self;
}

// OR, if the initializer hard-codes a param
- (id)init {
self = [super init];
if (self) {
_transitionMode = PopTransitionModeDismiss;
}
return self;
}

关于objective-c - 将枚举值分配给 Obj-C 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37449476/

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