【问题标题】:Can't install Realm library via CocoaPods in Swift 2.0无法在 Swift 2.0 中通过 CocoaPods 安装 Realm 库
【发布时间】:2015-10-09 03:15:08
【问题描述】:

我正在用 Swift2.0 制作 iOS 应用。

我尝试通过 CocoaPod 安装 Realm 库。

当我在命令行中执行“pod install”时就可以了。

但在 Xcode 项目中,我无法构建我的 iOS 应用程序。

出现错误并停止构建。

error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool: can't open file: /Users/satoutakeshi/Library/Developer/Xcode/DerivedData/AlermMA-hczduhcpoaudvlgzjrzioeecipfh/Build/Products/Debug-iphonesimulator/AlermMA.app/Frameworks/Realm.framework/Realm (No such file or directory)

这是我的 Podfile

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'RealmSwift'

我应该怎么做才能安装 Realm?

谢谢!

【问题讨论】:

  • 我的 iOS 项目是 Swift 和 Objective-C 的结合。
  • 您使用哪个版本的 CocoaPods?
  • 我使用的0.38.2版本。

标签: ios swift2 realm cocoapods


【解决方案1】:

您可能会看到此错误,因为在混合的 Objective-C/Swift 应用程序中不支持使用 RealmSwift pod。尝试使用 Objective-C Realm pod:

pod 'Realm'

您仍然可以在您的 Swift 文件中使用 Realm,您只需将 Objective-C 语法转换为 Swift,当然,RealmSwift 中只有 Swift 的东西将不可用。

【讨论】:

  • 感谢您的回答!我试过pod 'Realm',效果很好。
【解决方案2】:

如果您的项目使用 Swift,您必须安装 RealmSwift 才能使用完整的 swift 功能:

pod 'RealmSwift'

在您的代码中,您必须同时导入 Realm 和 RealmSwift 才能使用所有功能:

import Realm
import RealmSwift

编辑:

我更新了这个答案。感谢Donamite 指出您只需要在 podfile 中有 `RealmSwift'。

【讨论】:

  • 只有 pod 'RealmSwift' 就足够了,因为 Realm pod 是该 pod 的依赖项,事实上,在您的 Podfile 中同时包含这两者可能会导致问题,例如,如果 RealmSwift 是曾经依赖于与您在 Podfile 中指定的版本不同的版本。即,不要这样做:)
  • 您不必同时导入这两个框架。我什至不鼓励这样做,因为它会导致混乱。 import RealmSwift 就足够了。
  • 这就是我想的原因,但是当您想使用 RLMRealmConfiguration 时,如果您只导入 RealmSwift,则会出现编译器错误。它还想要import Realm
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 2018-08-02
  • 1970-01-01
  • 2021-02-02
相关资源
最近更新 更多