Ios Swift3:组合两个uint64类型选项

Ios Swift3:组合两个uint64类型选项,ios,swift,swift3,Ios,Swift,Swift3,用Swift 3写这个的正确方法是什么 let ld = NSDataDetector(types: NSTextCheckingResult.CheckingType.address | NSTextCheckingResult.CheckingType.phoneNumber) 这就是我得到的: 二进制运算符|不能应用于两个NSTextCheckingResult.CheckingType操作数 我知道它们都是UInt64,但我不知道如何组合它们。使用这些常量的原始值,因为类型Checki

用Swift 3写这个的正确方法是什么

let ld = NSDataDetector(types: NSTextCheckingResult.CheckingType.address | NSTextCheckingResult.CheckingType.phoneNumber)
这就是我得到的:

二进制运算符|不能应用于两个NSTextCheckingResult.CheckingType操作数


我知道它们都是
UInt64
,但我不知道如何组合它们。

使用这些常量的原始值,因为类型
CheckingType
不是int变量:

NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue)

NSTextCheckingResult.CheckingType.address中的地址是枚举大小写,而不是UInt64。原始值是UInt64,所以可以像这样使用原始值

do{

let ld = try NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue)

}catch{
    print("error")
}
试试这个

do {
    let ld = try NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue )
}
catch {

}

我个人会使用函数方法,通过使用
CheckingType
值数组。这将减少代码重复,并便于向扫描仪添加新的检查类型:

let detectorTypes = [
    NSTextCheckingResult.CheckingType.address,
    NSTextCheckingResult.CheckingType.phoneNumber
].reduce(0) { $0 | $1.rawValue }
let detector = try? NSDataDetector(types: detectorTypes)
或者,为了进一步减少值前缀中的重复:

let types: [NSTextCheckingResult.CheckingType] = [.address, .phoneNumber]
let detector = try? NSDataDetector(types: types.reduce(0) { $0 | $1.rawValue })

此外,它还可能抛出错误。。更好地使用try-catch块我用try-catch块编辑了答案