Realm – mobile database with Swift

What is Realm?
Realm is a mobile database
Realm is a replacement for SQLite & Core Data.
It can save you thousands of lines of code & weeks of work,
and lets you craft amazing new user experiences.

Download the Realm framework for swift from https://realm.io/
[code language=”obj-c”]
import UIKit
import RealmSwift
class Person: Object {
dynamic var name = ""
dynamic var age = 0
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Initializing the object
let personObj1 = Person()
personObj1.name = "Ashish"
personObj1.age = 25
println("name of person : \(personObj1.name)")
// Writing it to realm
let realm = Realm()
realm.write {
realm.add(personObj1)
}
let personObj2 = Person()
personObj2.name = "Darshak"
personObj2.age = 27
realm.write {
realm.add(personObj2)
}
// Quering
let r = Realm().objects(Person).filter("age > 24")
println(r)
// Queries are chainable
let r2 = r.filter("name contains ‘Ashish’")
println(r2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
[/code]
Output :
[code language=”obj-c”]
name of person : Ashish
Results<Person> (
[0] Person {
name = Ashish;
age = 25;
},
[1] Person {
name = Darshak;
age = 27;
}
)
Results<Person> (
[0] Person {
name = Ashish;
age = 25;
}
)
[/code]
Happy Coding 🙂