Got “is not a recognized Objective-C method” when bridging Swift to React-Native

前端 未结 3 1948
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 15:39

I\'m trying to bridge my React-Native 0.33 code to a super simple Swift method, following this guide but all I\'m getting is show:(NSString *)name is not a recognized

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 16:19

    This is a part of Swift 3's changes and can be solved by adding an underscore:

    import Foundation
    
    @objc(SwitchManager)
    class SwitchManager: NSObject {
    
      @objc func show(_ name: String) {
        NSLog("%@", name);
      }
    
    }
    

    See Swift 3's 0046 Proposal: Establish consistent label behavior across all parameters including first labels that is called out in the Swift.org migration guide under "Consistent first argument labels".

    Basically, how Objective-C sees Swift methods has changed with Swift 3.

    EDIT: This is still the case in Swift 4, see docs here under Omitting Argument Labels.

提交回复
热议问题