I'm trying to port some Obj-c code and having some trouble creating a NSDataDetector.
In Objective-C I would do this:
NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
From the documentation I should be able to do this:
let linkDetector = NSDataDetector.dataDetectorWithTypes(NSTextCheckingType.Link, error: &error)
But I get a compiler error: 'NSTextCheckingType' is not convertible to 'NStextCheckingTypes'
If try this:
let linkDetector = NSDataDetector.dataDetectorWithTypes(NSTextCheckingTypes(), error: &gError)
It passes however, I get a runtime exception:
[NSDataDetector initWithTypes:error:]: no data detector types specified'
Not sure if it's a bug or not.
Thanks.
Working solution for Swift 4:
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
Reference link to Apple Docs: https://developer.apple.com/documentation/foundation/nstextcheckingresult.checkingtype/1411725-link
NSTextCheckingTypes
is typealiased to UInt64; use the rawValue
property on an NSTextCheckingType
to convert it.
let ld = NSDataDetector(types: NSTextCheckingType.Link.rawValue, error: nil)
davextreme's solution returns an error in Xcode 6.1 (6A1046a).
Method 'fromRaw' has been replaced with a property 'rawValue'
New syntax uses rawValue
instead of toRaw()
as follows:
let ld = NSDataDetector(types:NSTextCheckingType.Link.rawValue, error: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With