iT邦幫忙

2025 iThome 鐵人賽

DAY 22
0
Mobile Development

Xcode x Swift Vibe coding進階開發之旅系列 第 22

Xcode x Swift Vibe coding進階開發之旅 第二十二天 點名系統(15) 改成關聯式資料庫

  • 分享至 

  • xImage
  •  

指令

/images/emoticon/emoticon08.gif

幫我改成關聯式資料庫

心得

因為只有修改資料表的部分,所以改寫完成後還需要請AI幫我們修改新增刪除編輯的部分。

程式

// 使用者資訊資料表
class User: Object {
    @Persisted(primaryKey: true) var userId: String = ""
    @Persisted var Name: String = ""
    @Persisted var createdAt = Date()
    @Persisted var active = true
    @Persisted var lastCheckInTime: Date? = nil
    
    // 關聯到此用戶的所有簽到記錄
    @Persisted var checkInRecords: List<CheckInRecord>
    
    convenience init(userId: String, name: String) {
        self.init()
        self.userId = userId
        self.Name = name
        self.active = true
    }
}

// 簽到記錄資料表
class CheckInRecord: Object {
    @Persisted(primaryKey: true) var id = UUID().uuidString
    @Persisted var checkInTime = Date()
    @Persisted var note: String? = nil
    
    // 建立與用戶的關聯
    @Persisted(originProperty: "checkInRecords") var user: LinkingObjects<User>
    
    convenience init(user: User, note: String? = nil) {
        self.init()
        // 將記錄添加到用戶的記錄列表中
        user.checkInRecords.append(self)
        self.note = note
    }
}

/images/emoticon/emoticon07.gif


上一篇
Xcode x Swift Vibe coding進階開發之旅 第二十一天 點名系統(14) 整理程式架構
系列文
Xcode x Swift Vibe coding進階開發之旅22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言