最近,在运行一个老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 命令安装即可。

到此这篇关于关于react native报cannot initialize a parameter of type’nsarray<id<rctbridgemodule>>错误(解决方案)的文章就介绍到这了,更多相关react native报错内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!