Install Realm in a Swift App

不打扰是莪最后的温柔 提交于 2019-12-04 10:25:23

I am not sure exactly why this isn't working, but here is a workaround:

  1. Follow the latest instructions.

  2. Create a bridging header, for example by

    • Add a new Objective-C class to your xcode project.
    • Agree to have a bridging header created
    • Delete the Objective-C class

  3. Add this in the bridging header:

    #import "Realm/Realm.h"

  4. Remove any Import Realm statements from your code, including from RLMSupport.swift

  5. Now it should work. For example, I test with putting this in my ViewController.swift

    import UIKit
    
    class Person: RLMObject {
        dynamic var name = ""
        dynamic var birthdate = NSDate(timeIntervalSince1970: 1)
    }
    
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let author = Person()
            author.name = "David Foster Wallace"
    
            // Get the default Realm
            let realm = RLMRealm.defaultRealm()
    
            // Add to the Realm inside a transaction
            realm.beginWriteTransaction()
            realm.addObject(author)
            realm.commitWriteTransaction()
    
            // Print all Persons
            println(Person.allObjects())
        }
    }
    

Which prints:

RLMArray <0x7a243760> (
    [0] Person {
        name = David Foster Wallace;
        birthdate = 1970-01-01 00:00:01 +0000;
    }
)

I have been talking with the guys at Realm, and it turns out that the latest instructions don't work with Realm <= 0.85 They changed they way the build the framework and it won't work anymore. They said they will release 0.86 later today that should fix the problems anyone is having with Swift. In the meantime I have a test project that anyone can take the latest framework from. https://github.com/smitt04/testRealm

Version 0.86 is now out and this is no longer an issue.

The Swift installation instructions were long and convoluted, so I'm not surprised you and several other users ran into issues.

Please follow the latest installation instructions here.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!