You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native iOS项目编译报错:React/RCTAssert.h文件未找到的持久化解决方案求助

彻底解决React Native iOS编译报错React/RCTAssert.h file not found

我帮你梳理下这个问题的根源,并且给出能彻底解决的方案——这个报错在RN 0.6x版本搭配社区组件时很常见,主要是Pod配置冲突、组件头文件引用适配问题,加上你之前的临时修复没从根本上解决,所以重装依赖后会复发。

问题根源分析

  1. 你的Podfile里存在重复的post_install脚本,而且在target内部的post_install里执行了target.remove_from_project移除了React target,这直接导致Xcode找不到React的头文件;
  2. use_frameworks!开启后,社区组件会被编译成动态库,但React 0.62默认是静态库,两者的头文件查找逻辑冲突,这也是RNCSlider找不到React头文件的核心原因;
  3. @react-native-community/slider 3.0.3版本和RN 0.62的头文件结构适配不佳,默认的引用路径不对。

彻底解决方案

步骤1:修复Podfile配置

打开你的iOS目录下的Podfile,做以下修改:

  1. 删除错误的React target移除逻辑
    找到target 'MyApp'内部的这段代码,直接删掉:
post_install do |installer|
  installer.pods_project.targets.each do |target|
    if target.name == 'React'
      target.remove_from_project
    end
  end
end

这段代码会把React从Pods项目里移除,必然导致头文件找不到。

  1. 调整pre_install脚本,兼容RNCSlider
    把pre_install里的组件列表加上RNCSlider,强制它编译为静态库,和React保持一致:
pre_install do |installer|
  installer.pod_targets.each do |pod|
    if pod.name.eql?('RNScreens') || pod.name.eql?('RNCMaskedView') || pod.name.eql?('RNCSafeAreaShadowView') || pod.name.eql?('RNCSlider')
      def pod.build_type
        Pod::BuildType.static_library
      end
    end
  end
end
  1. 合并重复的post_install脚本
    你的Podfile里有两段独立的installer.pods_project.targets.each脚本,把它们合并到最后的post_install里,避免冲突:
    最终的post_install应该是这样:
post_install do |installer|
  flipper_post_install(installer)
  installer.pods_project.targets.each do |t|
    t.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
      # 删除旧的部署目标配置,避免冲突
      config.build_settings.delete 'IPHONEOS_DEPLOYMENT_TARGET'
    end
  end
end

步骤2:持久化修复RNCSlider的头文件引用

因为每次npm install会覆盖node_modules里的文件,临时修改头文件没用,用patch-package做持久化修改:

  1. 安装patch-package:
npm install patch-package --save-dev
  1. 修改node_modules里的RNCSlider头文件引用:
    打开node_modules/@react-native-community/slider/ios/RNCSliderManager.m,把顶部的#import <React/RCTAssert.h>替换成:
#if __has_include("RCTAssert.h")
#import "RCTAssert.h"
#else
#import <React/RCTAssert.h>
#endif
  1. 生成持久化补丁:
npx patch-package @react-native-community/slider

这会在项目根目录生成patches/@react-native-community+slider+3.0.3.patch文件,以后每次npm install都会自动应用这个补丁。

步骤3:彻底清理并重装依赖

执行以下命令,彻底清理缓存和旧依赖:

# 清理Node.js依赖
rm -rf node_modules package-lock.json yarn.lock
npm install

# 清理iOS Pod依赖
cd ios
rm -rf Pods Podfile.lock
pod deintegrate
pod repo update
pod install

# 清理Xcode缓存
rm -rf ~/Library/Developer/Xcode/DerivedData

步骤4:验证Xcode配置

打开Xcode,检查项目的Build Settings:

  • 找到Header Search Paths,确保包含$(SRCROOT)/../node_modules/react-native/React,并且设置为Recursive;
  • 确认RNCSlider已经在你的项目Schema中(你之前已经做过这一步,再核对下)。

做完这些步骤后,重新编译项目,应该就不会再出现React/RCTAssert.h file not found的报错了。

内容的提问来源于stack exchange,提问作者Karthi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 09:43:00