ios - "unrecognized selector sent to instance"在 react-native 上设置属性时

标签 ios swift reactjs react-native

我正在尝试将我的 Swift View 与我的 React-Native 项目链接起来。我想出了如何显示它,但现在当我尝试设置一个属性时,我收到了这条错误消息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Switch setMessage:]: unrecognized selector sent to instance 0x7f96b270de70'

在我的 react-native 代码中,我正在做:

const SwitchNative = requireNativeComponent('Switch', Switch);

class Switch extends Component {
  render() {
    return (
        <SwitchNative message="message will be" style={this.props.style} />
    );
  }
}

然后在我的 SwiftBridge 中执行此操作:

// SwiftBridge.h

#import "RCTView.h"

@interface SwitchBridge : RCTView

  @property (nonatomic, assign) NSString *message;

@end

// SwiftBridge.m

#import "RCTBridgeModule.h"
#import "RCTViewManager.h"
#import "SwitchBridge.h"

@interface RCT_EXTERN_MODULE(SwitchManager, RCTViewManager)

  RCT_EXPORT_VIEW_PROPERTY(message, NSString)

@end

然后我的 Swift 类 Switch.swift 终于有了这个:

...
public func setMessage(message: String) {
  NSLog("It's working well");
}
...

不确定为什么找不到我的 setMessage 函数。

最佳答案

React Native 正在尝试调用 -[Switch setMessage:] 但您定义的 -[Switch setMessage] 没有任何参数。将参数添加到您的 Swift 方法签名:

public func setMessage(_ message: String)

根据您的 Swift 版本,您可能还需要使用 @objc 注释您的方法。

关于ios - "unrecognized selector sent to instance"在 react-native 上设置属性时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39967378/

相关文章:

ios - 是什么导致模拟器上 ios 8.1 和 ios 10.2 之间的后台 URLSession 出现奇怪的差异?

reactjs - 在react-select 2中显示带有选项的图像

reactjs - 在 React Native 中的每个获取请求之前附加自定义 header

javascript - 尝试访问 Reactjs 中的嵌套对象时出错

iOS 应用程序崩溃,没有任何错误消息

ios - Xcode Project 和 Playground 关于 Swift 错误处理的区别

iphone - 如何为 iPhone 构建不同高度图像的 3 列布局

ios - 如何以编程方式在 UIView 的中心添加文本?

iphone - 使用一个代码库 : framework for scaling layouts and assets for HTML5 apps on iPhones or iOS devices? 为非 Retina 和 Retina 显示器提供服务

ios - OpenCV - 无法访问 Mat 中的所有像素