React Native iOS集成Firebase遇'retain/strong属性需为对象类型'错误解决
解决React Native iOS集成Firebase时的
property with 'retain (or strong)' attribute must be of object type错误 问题场景
开发React Native iOS应用集成Firebase时,触发FirebaseStorage相关编译错误:
error: property with 'retain (or strong)' attribute must be of object type
完整错误日志如下:
In file included from /Users/xxxx/Documents/Grups/grups/node_modules/@react-native-firebase/messaging/ios/RNFBMessaging/RNFBMessagingSerializer.h:18: /Users/xxxx/Documents/Grups/grups/ios/Pods/Headers/Public/Firebase/Firebase.h:50:13: fatal error: could not build module 'FirebaseFirestore' #import <FirebaseFirestore/FirebaseFirestore.h> ~~~~~~~^ While building module 'FirebaseStorage' imported from /Users/xxxx/Documents/Grups/grups/ios/Pods/Headers/Public/Firebase/Firebase.h:78: In file included from <module-includes>:2: /Users/xxxx/Library/Developer/Xcode/DerivedData/grups-ekqodkfcidbeowgmbeplgbgpybif/Build/Products/Debug-iphonesimulator/FirebaseStorage/FirebaseStorage.framework/Headers/FirebaseStorage-Swift.h:315:1: error: property with 'retain (or strong)' attribute must be of object type
相关配置
Podfile配置
$RNFirebaseAsStaticFramework = true require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' platform :ios, '13.0' install! 'cocoapods', :deterministic_uuids => false target 'grups' do use_frameworks! :linkage => :static config = use_native_modules! # Flags change depending on the env values. flags = get_default_flags() use_react_native!( :path => config[:reactNativePath], # Hermes is now enabled by default. Disable by setting this flag to false. # Upcoming versions of React Native may rely on get_default_flags(), but # we make it explicit here to aid in the React Native upgrade process. :hermes_enabled => true, #:fabric_enabled => flags[:fabric_enabled], # Enables Flipper. # # Note that if you have use_frameworks! enabled, Flipper will not work and # you should disable the next line. #:flipper_configuration => FlipperConfiguration.enabled, # An absolute path to your application root. :app_path => "#{Pod::Config.instance.installation_root}/.." ) target 'grupsTests' do inherit! :complete # Pods for testing end post_install do |installer| react_native_post_install( installer, # Set `mac_catalyst_enabled` to `true` in order to apply patches # necessary for Mac Catalyst builds :mac_catalyst_enabled => false ) __apply_Xcode_12_5_M1_post_install_workaround(installer) end end
package.json依赖
{ "name": "grups", "version": "0.0.1", "private": true, "scripts": { "android": "react-native run-android", "ios": "react-native run-ios", "start": "react-native start", "test": "jest", "lint": "eslint ." }, "dependencies": { "react": "18.1.0", "react-native": "0.70.3", "@react-native-async-storage/async-storage": "^1.17.10", "@react-native-firebase/app": "^15.7.0", "@react-native-firebase/auth": "^15.7.0", "@react-native-firebase/firestore": "^15.7.0", "@react-native-firebase/messaging": "^15.7.1", "@react-native-firebase/storage": "^15.7.0", "@react-native-google-signin/google-signin": "^8.0.0", "@react-native-picker/picker": "^2.4.6", "@react-navigation/bottom-tabs": "^6.4.0", "@react-navigation/native": "^6.0.13", "@react-navigation/native-stack": "^6.9.0", "@types/react-native": "^0.70.4", "i18n-js": "^3.8.0", "react-native-date-picker": "^4.2.5", "react-native-image-crop-picker": "^0.38.0", "react-native-safe-area-context": "^4.4.1", "react-native-screens": "^3.17.0", "react-native-vector-icons": "^9.2.0" }, "devDependencies": { "@babel/core": "^7.12.9", "@babel/runtime": "^7.12.5", "@react-native-community/eslint-config": "^2.0.0", "babel-jest": "^26.6.3", "eslint": "^7.32.0", "jest": "^26.6.3", "metro-react-native-babel-preset": "0.72.3", "react-test-renderer": "18.1.0" }, "jest": { "preset": "react-native" } }
解决方案
这个错误源于Firebase Swift框架生成的头文件与Objective-C编译的兼容性冲突,结合你的配置,按以下步骤修复:
步骤1:统一Firebase依赖版本
当前@react-native-firebase各子包版本不一致(messaging为15.7.1,其余为15.7.0),且版本与React Native 0.70.x可能存在兼容问题。统一升级到同一系列的稳定版本,比如15.8.0:
- 修改
package.json中所有@react-native-firebase/*的版本为一致的版本:"@react-native-firebase/app": "^15.8.0", "@react-native-firebase/auth": "^15.8.0", "@react-native-firebase/firestore": "^15.8.0", "@react-native-firebase/messaging": "^15.8.0", "@react-native-firebase/storage": "^15.8.0" - 执行命令重新安装依赖:
npm install
步骤2:添加Firebase编译修复到Podfile
在Podfile的post_install钩子中加入针对Firebase Swift框架的编译配置,解决头文件属性错误:
修改后的post_install代码如下:
post_install do |installer| react_native_post_install( installer, :mac_catalyst_enabled => false ) __apply_Xcode_12_5_M1_post_install_workaround(installer) # 修复Firebase Swift框架的头文件属性错误 installer.pods_project.targets.each do |target| if target.name.start_with?('Firebase') && target.name != 'Firebase' target.build_configurations.each do |config| config.build_settings['SWIFT_OBJC_BRIDGING_HEADER'] = '' config.build_settings['SWIFT_OBJC_INTERFACE_HEADER_NAME'] = "$(PRODUCT_NAME)-Swift.h" config.build_settings['CLANG_WARN_DIRECT_OBJC_ISA_USAGE'] = 'NO' end end end end
步骤3:彻底清理缓存并重建
执行以下命令清理所有缓存,避免旧编译文件干扰:
# 清理Node.js依赖缓存 rm -rf node_modules package-lock.json yarn.lock # 切换到iOS目录清理Pods和Xcode缓存 cd ios rm -rf Pods Podfile.lock rm -rf ~/Library/Developer/Xcode/DerivedData # 重新安装依赖和Pods npm install pod install --repo-update
步骤4:检查Xcode编译设置
打开Xcode项目,确认以下设置:
- 项目的
Build Settings中,iOS Deployment Target设置为13.0或更高 Use Legacy Swift Language Version设置为No(建议使用Swift 5.5及以上版本)
内容的提问来源于stack exchange,提问作者fabio
相关产品推荐
相关产品推荐

