将Realm对象保存到DB

时间:2018-01-15 20:38:02

标签: ios swift realm

我正在尝试将Realm对象保存到数据库并遇到一个问题。当我运行项目时,我收到一个错误:

  

“dyld:未加载库:@ rpath / RealmSwift.framework / RealmSwift     参考自:/Users/levchukmisha/Library/Developer/CoreSimulator/Devices/6C527B07-299E-4B4F-B093-C2E79011AD3E/data/Containers/Bundle/Application/1DA8CC9D-FDEC-4A75-8B6D-19C3AF3508EF/BookShop.app/BookShop     原因:图片未找到“

我试图评论我的属性图像并检查会发生什么,但错误仍然相同。

这是我的功能:

func addBook() {
    let newBook = Book.createBook(
        name: "11 22 63",
        author: "Steven King",
        cost: 150,
        about: "Very interesting book with fantasty elements",
        image: UIImage(named: "112263")!)


    do {
        let realm = try Realm()
        try realm.write {
            realm.add(newBook)
            print("\(newBook.name)")
        }
    } catch {
        error.localizedDescription.description
    }


}

这是我的领域对象:

class Book: Object {

@objc dynamic var name = ""
@objc dynamic var author = ""
@objc dynamic var cost = 0
@objc dynamic var about = ""
@objc dynamic var image = UIImage()

class func createBook(name: String, author: String, cost: Int, about: String, image: UIImage) -> Book {
    let newBook = Book()
    newBook.name = name
    newBook.author = author
    newBook.cost = cost
    newBook.about = about
    newBook.image = image

    return newBook
}

}

1 个答案:

答案 0 :(得分:1)

确保将Realm.framework拖到项目常规设置选项卡中的“嵌入式二进制文件”部分。还要确保你有“嵌入框架”构建阶段,并包含Realm.framework

Installation section in documentation根据您使用的是CocoaPods还是Carthage

选择一个标签

请注意:

如果将Realm与Swift一起使用,请将Swift / RLMSupport.swift中的文件拖到Xcode项目的File Navigator中,选中Copy items if if needed复选框。