Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an rvalue of type

React Native 2021-11-11 阅读 279 评论 0

问题描述

Mac 升级到 macOS Big Sur 11.6,Xcode 升级到 13.0,在 Xcode 打开 React Native 项目,出现了错误:

/Volumes/develop/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:331:34: Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an rvalue of type 'NSArray<Class> *'

/Volumes/develop/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:776:79: Cannot initialize a parameter of type 'NSArray<Class> *' with an lvalue of type 'NSArray<id<RCTBridgeModule>> *__strong'

/Volumes/develop/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:827:69: Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an lvalue of type 'NSArray<Class> *__strong'

/Volumes/develop/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:331:34: Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an rvalue of type 'NSArray<Class> *'

/Volumes/develop/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:776:79: Cannot initialize a parameter of type 'NSArray<Class> *' with an lvalue of type 'NSArray<id<RCTBridgeModule>> *__strong'

/Volumes/develop/node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm:827:69: Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an lvalue of type 'NSArray<Class> *__strong'

package.js:

react: 16.11.0
react-native: 0.62.2

解决方法

参考 React Native 的官方提交,修改以下2个文件:

1. node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm

大概在 771 行,将

- (NSArray<RCTModuleData *> *)_initializeModules:(NSArray<id<RCTBridgeModule>> *)modules
                               withDispatchGroup:(dispatch_group_t)dispatchGroup
                                lazilyDiscovered:(BOOL)lazilyDiscovered

改为

- (NSArray<RCTModuleData *> *)_initializeModules:(NSArray<Class> *)modules
                               withDispatchGroup:(dispatch_group_t)dispatchGroup
                                lazilyDiscovered:(BOOL)lazilyDiscovered

2. node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm

diff --git a/node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm b/node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm
index 841f925..24f85ef 100644
--- a/node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm
+++ b/node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm
@@ -304,7 +304,7 @@ - (void)notifyAboutTurboModuleSetup:(const char *)name
             @"%@ has no setter or ivar for its bridge, which is not "
              "permitted. You must either @synthesize the bridge property, "
              "or provide your own setter method.",
-            RCTBridgeModuleNameForClass(strongModule));
+            RCTBridgeModuleNameForClass([strongModule class]));
       }
     }
最后更新 2021-12-23