if I declare the swift constant as a global constant like:
let a = \"123\"
but the a cannot be found in
Swift code:
public class MyClass: NSObject {
public static let myConst = "aConst"
}
and then in Objective-C:
[MyClass myConst]
Isn't this working as well? As in this works for me.
Also this is somewhat shorter as creating a object first (alloc, init). Making a new function for every constant is... not pretty :/
Because of the changes in Swift 4's Objective-C inference, you need to add the @objc annotation to the declared constant as well. The previous declaration then becomes:
@objcMembers
public class MyClass: NSObject {
public static let myConst = "aConst"
}
The calling Objective-C code remains the same.
Using
@objcMembersmakes all constants available (as if you'd write@objcbefore each constant), but I've had times where the compiler somehow wouldn't generate the corresponding ObjC code.In those cases I'd suggest adding the
@objcdecorator before the constant as well.
I.e.:@objc public static let myConst = "aConst"