Additional values ​​of non-Objective-C types are not bound to Objective-C. That is, the first three properties of TestClass below will be available in Objective-C, but the fourth will not:
class TestClass: NSObject { var nsNumberVar: NSNumber = 0
In Objective-C code, you get access to these first three properties, for example:
TestClass *optTest = [[TestClass alloc] init]; optTest.nsNumberOpt = @1.0; optTest.nsNumberVar = @2.0; optTest.doubleVar = 3.0;
In your case, you can convert lat and long to non-standard or switch them as instances of NSNumber .
Note that you need to be careful with your Objective-C code if you take the second approach (switching lat and lon to optional properties like NSNumber ) - while the Swift compiler will prevent you from assigning nil to optional properties, the Objective compiler- C has no doubt about its resolution, allowing nil values ​​to creep into your Swift code without any chance of catching them at runtime. Consider this method on TestClass :
extension TestClass { func badIdea() { // print the string value if it exists, or 'nil' otherwise println(nsNumberOpt?.stringValue ?? "nil") // non-optional: must have a value, right? println(nsNumberVar.stringValue) } }
This works great when calling values ​​in both properties, but if nsNumberVar set to nil from Objective-C code, it will crash at runtime. Please note that there is no way to check if there is nsNumberVar nil before using it!
TestClass *optTest = [[TestClass alloc] init]; optTest.nsNumberOpt = @1.0; optTest.nsNumberVar = @2.0; [optTest badIdea]; // prints 1, 2 optTest.nsNumberOpt = nil; optTest.nsNumberVar = nil; [optTest badIdea]; // prints nil, then crashes with an EXC_BAD_ACCESS exception
Nate Cook Oct. 14 '14 at 17:04 2014-10-14 17:04
source share