I want to access Swift code in Objective-c.
I have written one class in swift which contains static method. I want to access that Static Method in objective C Class.
Here is Class declaration:
@objc class LocalizedResource: NSObject {
/*!
* @discussion This function will get localize string for key
* @param key Localize key
* @return String for locaized key
* @code LocalizedResource.getStringForKey(key);
*/
static func getStringForKey(key:String) -> String {
let frameworkBundle = NSBundle.mainBundle()
let value = frameworkBundle.localizedStringForKey(key, value: nil, table: nil)
return value;
}
}
I have made following settings for it:
Also I have added @Obj before my class declaration in Swift class.
I have import MyProject-Swift.h in the .m file where I want to access that method.
But when I am trying to access it, it is not allowing me to access that static method.
Is any one having solution for it? Is there something missing?
Thnaks.
Swift static Methods In Swift, we use the static keyword to create a static method. For example, class Calculator { // static method static func add() { ... } } Here, add() is the static method.
Note: static methods can never be overridden! If you want to override a method, use class instead of static .
There is no difference really.
Static variables are those variables whose values are shared among all the instance or object of a class. When we define any variable as static, it gets attached to a class rather than an object. The memory for the static variable will be allocation during the class loading time.
Note in Swift 4 and the build setting swift 3 @objc inference
is default
, we must explicitly mark function as @objc
, or it will not generate class function in Swift bridge header.
From your comment:
I am calling this method as follows:
errorMessage.text = LocalizedResource.getStringForKey(@"TIMED_OUT_ERROR");
In Objective-C, the "dot syntax" is used for properties, not for methods. The correct call should be
errorMessage.text = [LocalizedResource getStringForKey:@"TIMED_OUT_ERROR"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With