iT邦幫忙

2022 iThome 鐵人賽

DAY 16
0
Mobile Development

IOS新手之旅系列 第 16

IOS新手之旅 Day16:鬧鐘(5)

  • 分享至 

  • xImage
  •  

昨天用完新增畫面了,今天來將資料存進Realm裡。

Realm架構

首先要先建立Realm的架構,內容大概如下。

import RealmSwift
import Foundation

class AlarmRealm: Object {

    @objc dynamic var ID = UUID().uuidString
    @objc dynamic var Hour: Int = 0
    @objc dynamic var Minute: Int = 0
    @objc dynamic var Sunday: Bool = false
    @objc dynamic var Monday: Bool = false
    @objc dynamic var Tuesday: Bool = false
    @objc dynamic var Wednesday: Bool = false
    @objc dynamic var Thursday: Bool = false
    @objc dynamic var Friday: Bool = false
    @objc dynamic var Saturday: Bool = false
    @objc dynamic var labelText: String = ""
    @objc dynamic var soundSelect: Int = 0
    @objc dynamic var switchOnOff: Bool = true
    
    override static func primaryKey() -> String? {
        return "ID"
    }
}

儲存

這裡在儲存Button裡打上要儲存的資料。

        let realm = try! Realm()
        let inputData = AlarmRealm()
        let cells = realm.objects(AlarmRealm.self)
      
        inputData.Hour = addPageValue.pageValue.pickHour
        inputData.Minute = addPageValue.pageValue.pickMinute
        inputData.Sunday = addPageValue.pageValue.repeatDayCheck[0]
        inputData.Monday = addPageValue.pageValue.repeatDayCheck[1]
        inputData.Tuesday = addPageValue.pageValue.repeatDayCheck[2]
        inputData.Wednesday = addPageValue.pageValue.repeatDayCheck[3]
        inputData.Thursday = addPageValue.pageValue.repeatDayCheck[4]
        inputData.Friday = addPageValue.pageValue.repeatDayCheck[5]
        inputData.Saturday = addPageValue.pageValue.repeatDayCheck[6]
        inputData.labelText = addPageValue.pageValue.labelText
        inputData.soundSelect = addPageValue.pageValue.soundSelect
        inputData.switchOnOff = addPageValue.pageValue.switchOnOff
            
        try! realm.write {
            realm.add(inputData)
        }

這樣就能將資料存進裡面了,明天就能在顯示畫面裡顯示了。


上一篇
IOS新手之旅 Day15:鬧鐘(4)
下一篇
IOS新手之旅 Day17:鬧鐘(6)
系列文
IOS新手之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言