Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install Realm in a Swift App

Tags:

ios

swift

realm

I am trying to add Realm to my app written in swift. I have followed the tutorial and I can't seem to get it to work. The biggest problem is that when I try to import Realm I get No such module 'Realm' I don't know what else to try. You can see my efforts below.

You can see the instructions here: http://realm.io/docs/cocoa/0.85.0/#swft

I have also copied the instructions below:

Due to the current lack of proper infrastructure for Swift dependency management, using Realm in your project requires the following steps:

  1. Add Realm as a submodule by opening the Terminal, cd-ing into your top-level project directory, and entering the command git submodule add [email protected]:realm/realm-cocoa.git
  2. Open the realm-cocoa folder, and drag Realm.xcodeproj into the file navigator of your Xcode project.
  3. In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the “Targets” section in the sidebar.
  4. In the tab bar at the top of that window, open the “Build Phases” panel.
  5. Expand the “Target Dependencies” gorup, and add Realm’s iOS framework.
  6. Expand the “Link Binary with Libraries” group, and add Realm’s iOS framework as well as libc++.dylib.
  7. Click on the + button at the top left of the panel and select “New Copy Files Phase”. Rename this new phase to “Copy Frameworks”, set the “Destination” to “Frameworks”, and add Realm.framework.
  8. Drag the file at realm-cocoa/Realm/Swift/RLMSupport.swift into the file navigator of your Xcode project, unchecking the “Copy items if needed” checkbox.

Below is what it looks like in my project:

enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

like image 527
smitt04 Avatar asked Dec 12 '22 03:12

smitt04


2 Answers

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;
    }
)
like image 113
Gusutafu Avatar answered Dec 29 '22 11:12

Gusutafu


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.

like image 39
smitt04 Avatar answered Dec 29 '22 11:12

smitt04