iOS(Swift 4)集成Firebase/Database报错:缺失archivedDataWithRootObject类方法
Hey there, let's tackle this frustrating Firebase SDK error you're hitting. That method archivedDataWithRootObject:requiringSecureCoding:error: is part of NSKeyedArchiver and was introduced in iOS 12.0—so the root cause usually boils down to compatibility between your project's deployment target, Swift version, and the Firebase/GoogleDataTransport pod versions you're using. Here are actionable steps to fix this:
1. Verify Your Project's Deployment Target
- Open your Xcode project, select your main target, and go to the General tab.
- Check the Deployment Info section: make sure your iOS deployment target is set to 12.0 or higher. If it's lower (like iOS 11 or earlier), the compiler won't recognize this iOS 12+ method.
- If you need to support older iOS versions (pre-12), skip to step 3 to lock compatible pod versions.
2. Align Podfile Platform Version with Your Project
- Open your
Podfileand ensure the opening platform declaration matches your project's deployment target. For example:platform :ios, '12.0' pod 'Firebase/Database' - A mismatch here can cause pods to compile against a different iOS SDK version than your project, leading to missing method errors.
3. Lock Compatible Firebase & GoogleDataTransport Versions
Since you're using Swift 4, you'll need to use Firebase versions that support Swift 4 and older iOS targets (if needed):
- Firebase 8.x is the last major version that supports Swift 4 and iOS 11+. Add this to your
Podfile:pod 'Firebase/Database', '~> 8.15.0' pod 'GoogleDataTransport', '~> 7.0.0' # This version is compatible with iOS 11+ - Run these commands to refresh your pods:
pod deintegrate rm -rf Podfile.lock pod install
4. Perform a Full Clean & Rebuild
Sometimes stale caches are the culprit. Try this complete cleanup process:
- Delete your project's Derived Data: Go to
Xcode > Preferences > Locationsand click the arrow next to Derived Data, then delete the folder for your project. - Clear CocoaPods caches:
rm -rf ~/Library/Caches/CocoaPods - Re-integrate pods from scratch:
pod deintegrate pod install - Finally, clean your project (
Cmd+Shift+K) and rebuild (Cmd+B).
5. Double-Check Scheme Configuration
You mentioned switching schemes, but make sure your active scheme is set to your app's target (not FirebaseCore or Firebase) when building. Those schemes are for Firebase's own development, not your app integration.
If none of these steps work, double-check that you haven't accidentally modified any Firebase-related source files (like GDTCORStorage.m)—but since you noted this is a core Objective-C file you didn't create, that's unlikely.
内容的提问来源于stack exchange,提问作者Adnan Afzal

