简体   繁体   English

iOS - 无法访问swift类的静态方法

[英]iOS - Not able to access Static method of swift class

I want to access Swift code in Objective-c . 我想在Objective-c中访问Swift代码

I have written one class in swift which contains static method. 我在swift中编写了一个包含静态方法的类。 I want to access that Static Method in objective C Class. 我想在目标C类中访问该静态方法。

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: 我为它做了以下设置:

  1. Product Module Name : MyProject 产品模块名称:MyProject
  2. Defines Module : YES 定义模块:是
  3. Embedded Content Contains Swift : YES 嵌入式内容包含Swift:是的
  4. Install Objective-C Compatibility Header : YES 安装Objective-C兼容性标头:是
  5. Objective-C Bridging Header : $(SRCROOT)/MySources/SwiftBridgingHeader.h Objective-C桥接标题:$(SRCROOT)/MySources/SwiftBridgingHeader.h

Also I have added @Obj before my class declaration in Swift class. 另外我在Swift类中的类声明之前添加了@Obj

I have import MyProject-Swift.h in the .m file where I want to access that method. 我在.m文件中导入MyProject-Swift.h,我想访问该方法。

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. Thnaks。

注意在Swift 4和构建设置swift 3 @objc inferencedefault ,我们必须明确地将函数标记为@objc ,否则它将不会在Swift桥头中生成类函数。

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. 在Objective-C中,“点语法”用于属性,而不用于方法。 The correct call should be 应该是正确的电话

errorMessage.text = [LocalizedResource getStringForKey:@"TIMED_OUT_ERROR"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM