Trouble passing Swift 4 String to Obj-C++ -[_SwiftValue dataUsingEncoding:]: unrecognized selector sent to instance

前端 未结 1 1912
無奈伤痛
無奈伤痛 2021-01-14 08:16

I have some Objective-C++ code like this:

// header

@interface MyObjcClass

- (void) myMethod: (NSString *) text;         


        
相关标签:
1条回答
  • 2021-01-14 08:27

    I'd try

    func mySwiftFunc(text: String) {
         myObjcClassInstance.perform(#selector(NSSelectorFromString("myMethod:"), with: text as NSString)
    }
    

    If that won't work try:

    func mySwiftFunc(text: String) {
        let selector : Selector = NSSelectorFromString("myMethod:")
        let methodIMP : IMP! = myObjcClassInstance.method(for: selector)
        unsafeBitCast(methodIMP,to:(@convention(c)(Any?,Selector,NSString)->Void).self)(myObjcClassInstance,selector,text as NSString)
    }
    

    Probably possible without selector name "myMethod:" String dependence, but I skipped the casting gymnastics as it's not the essence of the problem.

    0 讨论(0)
提交回复
热议问题