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:
- Add Realm as a submodule by opening the Terminal, cd-ing into your top-level project directory, and entering the command git submodule add git@github.com:realm/realm-cocoa.git
- Open the realm-cocoa folder, and drag Realm.xcodeproj into the file navigator of your Xcode project.
- 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.
- In the tab bar at the top of that window, open the “Build Phases” panel.
- Expand the “Target Dependencies” gorup, and add Realm’s iOS framework.
- Expand the “Link Binary with Libraries” group, and add Realm’s iOS framework as well as libc++.dylib.
- 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.
- 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:
I am not sure exactly why this isn't working, but here is a workaround:
Follow the latest instructions.
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
Add this in the bridging header:
#import "Realm/Realm.h"
Remove any Import Realm
statements from your code, including from RLMSupport.swift
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.