0

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.

sschale
  • 5,168
  • 3
  • 29
  • 36
Vanctor
  • 35
  • 1
  • 7

3 Answers3

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.

Community
  • 1
  • 1
sschale
  • 5,168
  • 3
  • 29
  • 36
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

Community
  • 1
  • 1
Muzahid
  • 5,072
  • 2
  • 24
  • 42
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