可以通过以下步骤实现:
@objc func passArrayToReactNative(_ array: [Any]) {
// 将数组数据传递给React Native
let bridge = RCTBridge.module(for: CustomModule.self)
bridge.eventDispatcher().sendAppEvent(withName: "ArrayDataEvent", body: array)
}
@objc func passArrayToReactNative(_ array: [Any]) {
// 将数组数据传递给React Native
let bridge = RCTBridge.module(for: CustomModule.self)
bridge.eventDispatcher().sendAppEvent(withName: "ArrayDataEvent", body: array)
}
import { NativeModules } from 'react-native';
// 调用Swift方法获取数组数据
const { CustomModule } = NativeModules;
CustomModule.passArrayToReactNative([1, 2, 3, 4, 5]);
// 监听Swift传递的数组数据
DeviceEventEmitter.addListener('ArrayDataEvent', (array) => {
console.log('Received array data from Swift:', array);
});
通过以上步骤,你可以成功将数组数据从Swift传递到React Native,并在React Native中获取到该数据。
领取专属 10元无门槛券
手把手带您无忧上云