ReactNative Advanced (Fifty) React Native reports Cannot initialize a parameter of type ‘NSArray<id<RCTBridgeModule>>Solution

I. Introduction

When using Xcode to run the RN project, the following error is reported:

Cannot initialize a parameter of type 'NSArray<id<RCTBridgeModule>> *' with an lvalue of type 'NSArray<Class> *__strong'

Cannot initialize a parameter of type 'NSArray<Class> *' with an lvalue of type 'NSArray<id<RCTBridgeModule>> *__strong'

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

The explanation given online is that the problem was caused after the upgradeXcode12.5, but by checking the projectjenkins build record, it was found that the last build was successful< The /span>, so the above statement is not true. version that failed this build, both are Xcode version is consistent with the XcodeXcode 13.2.1

2. Problem analysis and solutions

2.1 The first processing method: script batch modification of source files

Add the following script toios/Podfile the file:

post_install do |installer|
## Fix for XCode 12.5
  find_and_replace(
  "../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm",
  "_initializeModules:(NSArray<id<RCTBridgeModule>> *)modules", 
  "_initializeModules:(NSArray<Class> *)modules")
  
  find_and_replace(
  "../node_modules/react-native/ReactCommon/turbomodule/core/platform/ios/RCTTurboModuleManager.mm",
  "RCTBridgeModuleNameForClass(module))", 
  "RCTBridgeModuleNameForClass(Class(module)))"
  )
end

def find_and_replace(dir, findstr, replacestr)
  Dir[dir].each do |name|
      text = File.read(name)
      replace = text.gsub(findstr,replacestr)
      if text != replace
          puts "Fix: " + name
          File.open(name, "w") {
    
     |file| file.puts replace }
          STDOUT.flush
      end
  end
  Dir[dir + '*/'].each(&method(:find_and_replace))
end

Then, re-execute the pod install command.

If the version used by RN is too old and does not support CocoaPods ("react-native": "0.60.0" can only be used laterCocoaPods to integrate components), the second method can be used : Modify the source file directly.

2.2 The second processing method: directly modify the source file

Because eachreact native version has different names, some files may not be searchable.

  • react-native: Before 0.60.0: lower version;
  • react-native: After 0.60.0 (inclusive): higher version;

View the version of the current application through react-native -v and it is , so the higher version modification method is applied. react-native0.60.3

1. High version modification
../node_modules/react-native/React/CxxBridge/RCTCxxBridge.mm in the file

  _initializeModules:(NSArray<id<RCTBridgeModule>> *)modules

for

  _initializeModules:(NSArray<Class> *)modules


../node_modules/react-native/ReactCommon/turbomodule/core/platform/iOS/RCTTurboModuleManager.mm文文中訳

  RCTBridgeModuleNameForClass(module))

for

  RCTBridgeModuleNameForClass(Class(module)))

2. Lower version modification
My "react-native": "0.53.3" cannot be found

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

Will

- (void)_initModules:(NSArray<id<RCTBridgeModule>> *)modules

change into:

- (void)_initModules:(NSArray<Class> *)modules

../node_modules/react-native/ReactCommon/turbomodule/core/platform/iOS/RCTTurboModuleManager.mmThe file does not exist and has not been changed, but it can still run normally.

3. Extended reading

  • 《》

Guess you like

Origin blog.csdn.net/sunhuaqiang1/article/details/134873561