备注:这不是this question的骗局。NS_ENUM in Swift
我想在Swift中使用名为GBCli的库。它是用Objective-C编写的。我有一个特定的枚举麻烦:
/** Various command line argument value requirements. */
typedef NS_ENUM(NSUInteger, GBValueFlags) {
GBValueRequired, ///< Command line argument requires a value.
GBValueOptional, ///< Command line argument can optionally have a value, but is not required.
GBValueNone ///< Command line argument is on/off switch.
};
我试图使用方法:
- (void)registerOption:(NSString *)longOption shortcut:(char)shortOption requirement:(GBValueRequirements)requirement;
在文档中,建议使用方法是:
[parser registerOption:@"verbose" shortcut:'v' requirement:GBValueRequired];
我试着翻译这作为:
parser.registerOption("verbose", shortcut: 118 /* Array("v".utf8)[0] */, requirement: .Required);
注意:看起来GBValueRequirements
是库使用的参数类型,但是GBValueFlag
是他们打算传递给所述方法的实际类型。证据:typedef NSUInteger GBValueRequirements;
,上面提到的枚举,以及许多函数采用GBValueRequirements
的事实。还请注意,GBValueFlags
中的每个案例都是排他性的,因此它们并不意味着“或”在一起。
但是,这给出了一个错误Type 'UInt' has no member 'Required'
,这是没有意义的看到该枚举被定义为类型NSUInteger
(它应该转换为UInt)。我能够访问GBValueFlags,但我无法看到任何迅速翻译它。奇怪的是,这个工程:
let requiredTest : GBValueFlags = .Required;
但这并不:
parser.registerOption("verbose", shortcut: 118 /* Array("v".utf8)[0] */, requirement: requiredTest);
因为它抛出:
Cannot convert value of type 'GBValueFlags' to expected argument type 'UInt'
在编译时。我应该如何通过.Required
的等效方法?
定义我自己的枚举不起作用,因为GBCli的内部代码根据自己的枚举检查参数。
怎么样'parser.registerOption( “详细”,便道:118/*阵列( “V” .utf8)[0] * /,要求:GBValueFlags.Required.rawValue)'? –
@JonBrooks谢谢!作为回答发布,我会接受。 – Coder256