我想用Realm製作3D數組,並使用List來完成此操作。 這是我RealmDatabase代碼:Realm Swift中的多維數組(最大深度超出錯誤)
class Sensors : Object {
dynamic var type = ""
dynamic var name = ""
let buttonAr = List<buttons>()
}
class buttons: Object{
let buttonsAdded = List<objectNameTopic>()
}
class objectNameTopic: Object {
dynamic var name = ""
dynamic var topic = ""
}
這是我用來稱呼它:
var saving = Sensors()
var saving2 = objectNameTopic()
var but = buttons()
var array = ["on", "off"]
var array2 = ["1","2"]
var allSensors = useOfRealm.objects(Sensors.self)
override func viewDidLoad() {
super.viewDidLoad()
addSensor()
for i in 0...array.count-1 {
addSensor2(name: array[i], topic: array2[i])
}
if allSensors.count > 0 {
print(allSensors)
}
}
func addSensor() {
try! useOfRealm.write {
saving.type = "topic1"
saving.name = "mike"
useOfRealm.add(saving)
}
}
func addSensor2(name: String, topic: String) {
try! useOfRealm.write {
saving2.name = name
saving2.topic = topic
useOfRealm.add(saving2)
but.buttonsAdded.append(saving2)
saving.buttonAr.append(but)
}
}
這是我所得到的,當我打印結果:
Results<Sensors> (
[0] Sensors {
type = topic1;
name = mike;
buttonAr = RLMArray <0x6100000fd900> (
[0] buttons {
buttonsAdded = RLMArray <0x6100000fdb00> (
[0] <Maximum depth exceeded>,
[1] <Maximum depth exceeded>
);
},
[1] buttons {
buttonsAdded = RLMArray <0x6180000fd180> (
[0] <Maximum depth exceeded>,
[1] <Maximum depth exceeded>
);
}
);
}
)
任何我想念的想法?
在此先感謝
你只是實例化一個'saving'和一個'saving2'實例,並重復使用它們多次。這可能不是問題的原因,但Realm是一個對象數據庫,所以您應該每次都要實例化一個新對象。這同樣適用於'but',儘管目前還不清楚它爲什麼存在,而不僅僅是'Sensors'上的列表屬性。 – Michael
你是對的,我在幾個小時前發現它,當時我試圖用一個for填充數據庫,它只返回最後一項,因爲它覆蓋了其他所有內容。我會嘗試應用這些代碼的變化,看看會發生什麼 –