关于React Native报Cannot initialize a parameter of type'NSArray<id<RCTBridgeModule>>错误(解决方案)

最近,在运行一个老RN项目的时候,使用Xcode运行的时候报了如下的代码错误:

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> *\’

这是由于升级XCode 12.5之后的问题,在ios/Podfile文件中加入如下的脚本即可。

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

然后,重新执行pod install 命令安装即可。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容