为什么我总是收到错误 "no such module 'realmswift'"?

Why do I keep getting the error "no such module 'realmswift'"?

这个问题之前的答案已经超过一年了。我已经试过了。他们真的不工作。我已经成功安装了使用 Realm 的 pod。我在 Realm 数据库中写入了一些用户名和角色,我可以在 Realm Studio 中看到这些信息。然后我离开我的电脑一个小时(在这段时间里没有其他人碰过它)。当我回来时,我得到了没有这样的模块错误。

我已经尝试通过终端多次更新 pod 文件。我也退出 Xcode 并重新启动它。我还 Google-ed 寻找解决方案并尝试通过“产品”下拉列表添加新方案。虽然这将使我的项目成功编译,但新方案不会在模拟器上启动。

这是我的播客文件:

# Uncomment the next line to define a global platform for your project
# platform :ios, '12.2'

target 'realmProject' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for realmProject
pod 'RealmSwift'

end

这是给我错误的 ViewController:

import UIKit
import RealmSwift

class SecondViewController: UIViewController {
    @IBOutlet weak var nameTextField: UITextField!

    @IBOutlet weak var rolePickerView: UIPickerView!

    let roles = ["Designer", "Developer"]

    override func viewDidLoad() {
        super.viewDidLoad()
        rolePickerView.dataSource = self
        rolePickerView.delegate = self
    }

    var employees = Employee()

    @IBAction func addButtonPressed(_ sender: Any) {


        employees.name = nameTextField.text ?? "no name"

        employees.role = roles[rolePickerView.selectedRow(inComponent:     0)]

        let realm = try! Realm()

        try! realm.write {
            realm.add(employees)
        }

        let results = realm.objects(Employee.self)

        for r in results {
            employees.append(r)
        }

        print("button pressed")

    }
}

extension SecondViewController: UIPickerViewDelegate, UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return roles[row]
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return roles.count
    }
}

我只是想使用 Realm 来保存数据并使用它来填充表格视图。现在我收到上面的错误,说我不能使用 Realm。

与其他 pods 有同样的问题,例如 Firebase。

Quickfix:转到编辑器,清理构建文件夹,然后 select Pods 项目并为通用 iOS 设备构建它。之后,为您想要的目标再次构建您的项目。这有时可以解决 "no such module" 的错误。

如果这不起作用,请在终端上关闭 Xcode、运行 rm -rf ~/Library/Developer/Xcode/DerivedData/,再次打开 Xcode 并重新构建。

解决此问题的最佳方法是执行以下步骤

1) 在 xcode 中单击项目或产品,然后单击管理方案

2) 取消选中您的项目并检查您已安装的 pod 后

3) 使用 pod 构建您的应用程序

4) 再次更改您的方案并取消选中 pod 并检查您的应用程序后,这会起作用,我添加了图片来解释这一点:)

像这样建造