I am trying to access a Swift Extension to NSString from an objective-C class using the @objc prefix but can't access an optional.
Now that Swift 4.2 requires @objc before variables or functions called from Objective-C, to access the extension, I have to add the @objc extension.
While this works fine with non optionals--I use it extensively in my project, adding @objc to the following extension is giving the error:
I'm not sure but I think it may be because CLLocationCoordinate is an optional.
Can anyone see what is wrong with the code? Thanks for any suggestions.
If I don't put @objc in following, I can't access it from Objective-C. Error is: Property 'asLocationCoord' not found on object of type 'NSString
If I do put it in, Swift compiler complains 'Property cannot be marked @objc because its type cannot be represented in Objective-C'
public extension NSString {
@objc public var asLocationCoord: CLLocationCoordinate2D? {
var mylocation: CLLocationCoordinate2D? = nil
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(self as String) { (placemarks, error) in
guard
let placemarks = placemarks,
let firstlocation = placemarks.first?.location?.coordinate
else {
// handle no location found
return
}
mylocation = firstlocation
}
return mylocation
}//end var
}//end extension