我有一个 Objective-C 对象,其属性类型为 VerticalAlignment
。 VerticalAlignment
是一个枚举,由整数支持:
typedef NS_ENUM(unsigned int, VerticalAlignment)
{
kVerticallyAlignMiddle = 0,
kVerticallyAlignTop,
kVerticallyAlignBottom
};
@property VerticalAlignment alignment;
我想从 Swift 对象设置对齐
:
if let textGraphic = internalTextGraphic {
textGraphic.alignment.rawValue = newValue
}
但是我rawValue
是不可变的。
有什么方法可以传递alignment
一个整数来设置吗?
最佳答案
textGraphic.alignment = VerticalAlignment(rawValue: newRawValue)!
请注意VerticalAlignment.init(rawValue:)
返回 Optional<VerticalAlignment>
,如果原始值无效,则该值将为 nil。所以如果你传递了一个假的newRawValue
,程序在尝试解开 nil 时会崩溃。如果您想避免这种情况:
textGraphic.alignment = VerticalAlignment(rawValue: newRawValue) ?? textGraphic.alignment
或
if let alignment = VerticalAlignment(rawValue: newRawValue) {
textGraphic.alignment = alignment
}
关于swift - 我可以使用 Int 从 Swift 设置 Int 枚举类型的 Objective-C 属性吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35805315/