Sie sind auf Seite 1von 5

iOS Swift 5 Implementation Tutorial

1. Install CocoaPods 1.1.0 or later.


2. Run pod repo update to make CocoaPods aware of the latest available
Realm versions.
3. In your Podfile, add use_frameworks! and pod 'RealmSwift' to your main
and test targets.
4. From the command line, run pod install.
5. Use the .xcworkspace file generated by CocoaPods to work on your
project!

?
1 platform :ios, ‘9.0’
use_frameworks!
2
3
source 'https://github.com/CocoaPods/Specs.git'
4
5
target 'MyApp' do
6 pod 'RealmSwift'
7 end

1
2 import RealmSwift
3
4 class Person: Object {
5
6 private(set) dynamic var id = 0
private(set) dynamic var name = ""
7 private(set) dynamic var email = ""
8
9 /**
10 Override Object.primaryKey() to set the model’s primary key. Declaring a primary
11value.
*/
12 override static func primaryKey() -> String? {
13 return "id"
14 }
15
16 convenience init(id: Int, name: String, email: String) {
17 self.init()
18
self.id = id
19 self.name = name
20 self.email = email
21 }
22}
23
Also, we need a RealmManager class to manage the realm
operations, like delete, save, get and so on. For a brief presentation, I've
added only the following three methods:
?
1
2
3 import RealmSwift
4
5 class RealmManager {
6
7 let realm = try! Realm()
8
9 /**
Delete local database
10
*/
11 func deleteDatabase() {
12 try! realm.write({
13 realm.deleteAll()
14 })
}
15
16 /**
17 Save array of objects to database
18 */
19 func saveObjects(objs: [Object]) {
20 try! realm.write({
// If update = true, objects that are already in the Realm will be
21 // updated instead of added a new.
22 realm.add(objs, update: true)
23 })
24 }
25
26 /**
Returs an array as Results<object>?
27 */
28 func getObjects(type: Object.Type) -> Results<object>? {
29 return realm.objects(type)
30 }
}</object></object>
31
32
33
You can notice that when we save in the realm database, we don't need to
specify the object type, but when we retrieve the objects we need to specify
the object meta type that we want to be retrieved.

In Swift, the meta type of a type can be found by calling .self on a


type. The returned type can either be a Class meta-type, a Struct meta-type
or a Protocol meta-type. In our case, the object returned from calling
Person.self is the Swift metaType of Person class.

On the TestClass is generated an array of Person that will be saved


on the realm database. Certainly you can save objects individually and then
get them individually, but on this demo scenario I chose to save and get
objects as a list.
?
1
2
3 class TestClass {
4
5 let realm = RealmManager()
6
7 var persons = [Person]()
8
9 func testAll() {
addTestPersons()
10
11 // save persons array to database
12 realm.saveObjects(objs: persons)
13
14 // get persons array from database
15 getObjects()
16 }
17
func addTestPersons() {
18 for index in 0...9 {
19 let newPerson = Person(id: index, name: "Name\(index)", email: "p\(ind
20 persons.append(newPerson)
21 }
22 }
23
func getObjects() {
24 if let objects = realm.getObjects(type: Person.self) {
25 for element in objects {
26 if let person = element as? Person {
27 // Do whatever you like with 'person' object
print("\(person.name), \(person.id), \(person.email)")
28 }
29 }
30 }
31 }
32 }
33
34
When we get all objects of Person type, we will obtain an array of
Results<Object>
Type. Next we can loop through each element of array using optional
binding to recreate the Person object or we can directly downcast to to
Person type using as! Swift operator. In this scenario I only printed the
result on Xcode console, but you can do whatever you want with the Person
object, like to recreate the persons array to be shown in a table view.
?
1 Name0, 0, p0@gmail.com
Name1, 1, p1@gmail.com
2 Name2, 2, p2@gmail.com
3 Name3, 3, p3@gmail.com
4 Name4, 4, p4@gmail.com
5 Name5, 5, p5@gmail.com
6 Name6, 6, p6@gmail.com
Name7, 7, p7@gmail.com
7 Name8, 8, p8@gmail.com
8 Name9, 9, p9@gmail.com
9
10
Impressions
References
You can checkout more about Realm on the official page https://realm.io/ and
https://realm.io/docs/.

Das könnte Ihnen auch gefallen