Reputation: 5076
What's the proper way to write this in Swift 3?
let ld = NSDataDetector(types: NSTextCheckingResult.CheckingType.address | NSTextCheckingResult.CheckingType.phoneNumber)
This is what I get:
Binary operator | cannot be applied to two NSTextCheckingResult.CheckingType operands.
I know they're both UInt64
's, but I have no idea how to combine them.
Upvotes: 2
Views: 118
Reputation: 32853
I'd personally go with a functional approach, by using an array of CheckingType
values. This will reduce the code duplication and makes it easy to add new check types to the scanner:
let detectorTypes = [
NSTextCheckingResult.CheckingType.address,
NSTextCheckingResult.CheckingType.phoneNumber
].reduce(0) { $0 | $1.rawValue }
let detector = try? NSDataDetector(types: detectorTypes)
Or, to further reduce the duplications in the values prefixes:
let types: [NSTextCheckingResult.CheckingType] = [.address, .phoneNumber]
let detector = try? NSDataDetector(types: types.reduce(0) { $0 | $1.rawValue })
Upvotes: 0
Reputation: 3310
Try this
do {
let ld = try NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue )
}
catch {
}
Upvotes: 1
Reputation: 874
address in NSTextCheckingResult.CheckingType.address is a enum case, not UInt64. The raw value is UInt64, so you can use raw value like this,
do{
let ld = try NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
}catch{
print("error")
}
Upvotes: 0
Reputation: 13713
Use the raw value of these constants as the type CheckingType
is not an int variant:
NSDataDetector(types: NSTextCheckingResult.CheckingType.address.rawValue | NSTextCheckingResult.CheckingType.phoneNumber.rawValue)
Upvotes: 0