rigdonmr
rigdonmr

Reputation: 2702

iOS: Writes to Realm in swift 1.2

I'm trying to instantiate a realm to perform a write in Realm using the given method in the docs:

let realm = try! Realm()

But I get the following error:

Consecutive statements on a line must be separated must be separated by ';'

Given that Realm is really built for Swift 2.0, I'm assuming the try! keyword isn't supported in Swift 1.2 (the version I'm using), but Realm states that it supports 1.2, but don't provide any documentation on how to do it in v 1.2.

Maybe I'm wrong that it's a swift version problem? Anyone know what the problem is? Thanks.

Upvotes: 2

Views: 80

Answers (2)

Michal
Michal

Reputation: 15669

You need to download the Swift 1.2 version available in this branch.

Upvotes: 2

Tejas Patel
Tejas Patel

Reputation: 870

Please Follow the instructions.

Create a bridging header,

example,

1) Add a new Objective-C class to your xcode project.

2) Agree to have a bridging header created

3)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(timeIntervalSince1990: 1)
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let person = Person()
        person.name = "ANY_NAME"

        // 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())
    }
}

Output:

RLMArray <0x9a675890> (
[0] Person {
    name = ANY_NAME;
    birthdate = 1990-01-01 00:00:01 +0000;
}
)

Upvotes: -1

Related Questions