I have declare a property var coordinate:CLLocationCoordinate2D?
in my swift class
but i can't find this property in obj-c class. I have tried to add @obj
before the class, but it doesn't work.
Asked
Active
Viewed 306 times
0
-
If you make it non-optional it will work. (`CLLocationCoordinate2D`) – nielsbot Mar 14 '16 at 08:05
3 Answers
0
The problem is that you have declared an optional
property (?
), which is a pure Swift
construct. They cannot be accessed by Objective C
classes unless specifically bridged, like String
-> NSString
.
0
You can not access the instance variable because coordinate:CLLocationCoordinate2D is not bridged with Objective C. Instead of declaring CLLocationCoordinate2D, you can declare latitude and longitude as NSNumber type and later using them make 2D coordinate.
var lat: NSNumber?
var lon: NSNumber?
And then assign value in your Objc Class as bellow:
switObject.lat = @3;
switObject.lon = @5;
For more information check this link Cannot access property of Swift type from Objective-C
0
Obj-C doesn't support optional structs. But if you make it non-optional, it compiles:
import CoreLocation
class Class : NSObject
{
dynamic var coordinate:CLLocationCoordinate2D // no `?`: non-optional
override init()
{
coordinate = CLLocationCoordinate2D() // it's not optional, so you must initialize `coordinate`
}
}

nielsbot
- 15,922
- 4
- 48
- 73