A.I.A.
A.I.A.

Reputation: 189

Realm: please show how to properly create objects

I have RealmData.swift with two classes:

import UIKit
import RealmSwift

class Task : Object {
    dynamic var taskName = ""
    dynamic var taskStatus = ""
}

class TaskList : Object {
    dynamic var listName = ""
    let tasks = List<Task>()
}

Now I'm trying to implement demo fill in for Realm to show it later in my UITableViewController:

import UIKit
import RealmSwift

class ListsTableViewController: UITableViewController, ListCellDelegate, UITextFieldDelegate {
// (...)
override func viewDidLoad() {
    // (...)
    let list_0 = TaskList(value: ["Things for vaction", [["Cash & Cards", false], ["Clothes", false], ["Passport", false] ] ])
    let realm = try! Realm()
    realm.write {
        realm.add(list_0)
    }
}
}

This code should by design create a List with name "Things for vacation" having 3 tasks with names "Cash & Cards", "Clothes" and "Passport" and false taskStatus'es. When I add "let list_0 = (...)" line and run, app crashes on start with "Thread 1: signal SIGABRT" exception. Am I misspelling something or may be it's required to create Task objects first? But https://realm.io/docs/swift/latest/ has the same example:

let aPerson = Person(value: ["Jane", 30, [["Buster", 5], ["Buddy", 6]]])

Explain please, what is wrong with this? I'm fine with Current Limitations (https://realm.io/docs/swift/latest/#current-limitations)

Thanks in advance!

Upvotes: 2

Views: 695

Answers (1)

kishikawa katsumi
kishikawa katsumi

Reputation: 10573

It doesn't match the type of the property.

You pass array object for Task as ["Cash & Cards", false]. The array contains String and Bool value. Otherwise, your model definition of Task has only String properties. So the latter value doesn't match, pass the boolean value but model expects String value.

So the solution is: make your model containing String and Bool properties. Like below

class Task : Object {
    dynamic var taskName = ""
    dynamic var taskStatus = false
}

Upvotes: 1

Related Questions