非藉秋风

导航

OC调用Swift的String扩展方法

public extension NSString {

    @objc func swiftMethod() -> Bool {

        let hello = "hello swift"

        return hello

    }

}

参考自:

https://stackoverflow.com/questions/27097688/can-objective-c-code-call-swift-extension-on-class

An exception to this is when the category is defined on a bridged type (i.e. the extension is defined on String and not NSString). These categories will not automatically be bridged to their Objective-C counterparts. To get around this, you'll either need to use the Objective-C type (and cast the return value in your Swift code with as String) or define an extension for both the Swift and Objective-C types.

posted on 2020-12-29 10:23  非藉秋风  阅读(1012)  评论(0编辑  收藏  举报