简体   繁体   中英

Save Realm object to DB

I'm trying to save a Realm object to database and have one problem. When I run project i'm getting an error:

"dyld: Library not loaded: @rpath/RealmSwift.framework/RealmSwift Referenced from: /Users/levchukmisha/Library/Developer/CoreSimulator/Devices/6C527B07-299E-4B4F-B093-C2E79011AD3E/data/Containers/Bundle/Application/1DA8CC9D-FDEC-4A75-8B6D-19C3AF3508EF/BookShop.app/BookShop Reason: image not found"

I tried to comment my property image and check what happens, but error still the same.

Here is my function:

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
    }


}

And here is my realm object:

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
}

}

Make sure you dragged Realm.framework to the "Embedded Binaries" section in your project's General settings tab. Also make sure you have "Embed Frameworks" build phase and Realm.framework is included there

Installation section in documentation select a tab according whether you're using CocoaPods or Carthage

Take note of this :

If using Realm with Swift, drag the file at Swift/RLMSupport.swift into the File Navigator of your Xcode project, checking the Copy items if needed checkbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM