Reputation: 87
Currently i need to use my Swift global variables in Objective-c code. These are my variables:
public var Name = [String]()
public var Author = [String]()
public var Url = [String]()
This is a class that returns my public variables:
@objc class AppConstant: NSObject {
private override init() {}
class func name() -> [String] { return Name }
class func author() -> [String] { return Author}
class func url() -> [String] { return Url}
}
My Objective-C code:
@implementation SecondViewController
NSArray *urlArray;
NSArray *nameArray;
NSArray *authorArray;
- (void)viewDidLoad {
[super viewDidLoad];
urlArray = [AppConstant url];
nameArray = [AppConstant name];
authorArray = [AppConstant author];
}
@end
I think everything should be okay, but it returns error
No known class method for selector 'url'/'name'/'author'.
Can someone explain where is error. I did everything as explained here, but it doesnt works. Hope someone will help me
Thank you in advance!
Upvotes: 3
Views: 1820
Reputation: 318955
You need @objc
added to any property or method that you want exposed to Objective-C.
@objc class AppConstant: NSObject {
private override init() {}
@objc class func name() -> [String] { return Name }
@objc class func author() -> [String] { return Author}
@objc class func url() -> [String] { return Url}
}
Or you can use @objcMembers
if you want to expose all non-private properties and methods:
@objcMembers class AppConstant: NSObject {
private override init() {}
class func name() -> [String] { return Name }
class func author() -> [String] { return Author}
class func url() -> [String] { return Url}
}
This is a change in Swift 4 from Swift 3. And note that the old question you linked is 4 years old. Swift has changed a lot since then.
Upvotes: 5