领域:请显示如何正确创建对象

时间:2021-06-17 02:51:25

I have RealmData.swift with two classes:

我有RealmData。斯威夫特有两类:

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:

现在我正在尝试实现demo fill in for Realm后面在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:

这段代码应该通过设计创建一个列表,上面写着“度假的东西”,有3个任务,名字是“现金和卡片”,“衣服”和“护照”,还有假任务状态。当我添加“让list_0 =(…)”行并运行时,应用程序在启动时崩溃,“线程1:信号SIGABRT”异常。是我拼错了什么,还是需要先创建任务对象?但是https://realm。io/docs/swift/latest/都有同样的例子:

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)

请解释一下,这有什么问题?我对当前的限制很好(https://realm.io/docs/swift/latest/# Current -limit)

Thanks in advance!

提前谢谢!

1 个解决方案

#1


1  

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.

您将任务的数组对象作为["Cash & Cards", false]传递。数组包含字符串和Bool值。否则,您的任务模型定义只有字符串属性。所以后一个值不匹配,传递布尔值,但是模型需要字符串值。

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

因此解决方案是:使您的模型包含字符串和Bool属性。像下面的

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

#1


1  

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.

您将任务的数组对象作为["Cash & Cards", false]传递。数组包含字符串和Bool值。否则,您的任务模型定义只有字符串属性。所以后一个值不匹配,传递布尔值,但是模型需要字符串值。

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

因此解决方案是:使您的模型包含字符串和Bool属性。像下面的

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