gpt4 book ai didi

swift - 如何编写Realm 嵌套对象?

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:49 28 4
gpt4 key购买 nike

我有这个 Realm 结构:

class DateLog: Object {
dynamic var date = NSDate()
let workouts = List<WorkoutLog>()
let activities = List<ActivityLog>()
}

class WorkoutLog: Object {
dynamic var name = ""
dynamic var value = 0
dynamic var notes = ""
}

class ActivityLog: Object {
dynamic var name = ""
dynamic var value = 0
dynamic var notes = ""
}

我如何将 DateLog > WorkoutLog 数据添加到 Realm ?(DateLog 的日期;WorkoutLog 的名称、值、注释)?

我正在寻找的工作流程:

  1. 将新记录添加到日期日志(只是一个日期)
  2. 添加该日期的锻炼日志记录(包含所有详细信息)
  3. 添加同一日期的事件日志记录

最佳答案

只需在写入 block 中创建 DateLog 对象并将其添加到 Realm:

let realm = try! Realm()

try realm.write {
var dateLog = DateLog()
dateLog.date = NSDate()

var workoutLog = WorkoutLog()
workoutLog.name = "My Workout"
workoutLog.value = 1
workoutLog.notes = "my note"

dateLog.workouts.append(workoutLog)

realm.add(dateLog)
}

关于swift - 如何编写Realm 嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36232092/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com