ios – 从Objective-C访问Swift扩展

我无法从 objective-c访问我的swift扩展.

我在.swift文件中有以下代码:

extension NSDictionary {
    func dictionaryAsJsonString() -> NSString {
        var err: NSError?
        var data = NSJSONSerialization.dataWithJSONObject(self,options: NSJSONWritingOptions.PrettyPrinted,error: &err)
        var string = NSString(data: data,encoding: NSUTF8StringEncoding)
        return string
    }
}

我希望能够在我的.m文件中执行以下操作:

[dictionary dictionaryAsJsonString];

但它找不到我的方法,也没有自动完成.

我知道我的导入工作正常,因为我能够访问我的其他swift对象.

解决方法

使用简单的词典很容易
20> extension Dictionary {
 21.     func toJSONString () -> String { return "my dictionary" }
 22. }
 23> ["a": 1,"b": 2].toJSONString()
$R10: String = "my dictionary"

Apple文档没有提到在Objective-C类上使用扩展.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...