iT邦幫忙

2023 iThome 鐵人賽

DAY 9
0

留言板我們先坐輸入儲存近realm裡,在輸入完成之後,按下確認鍵之後將資料先寫入realm

@IBAction func enter(_ sender: Any) {
        let timeInterval: TimeInterval = Date().timeIntervalSince1970
        timeTemp = Int(timeInterval)
        let userstring = userInput.text!
        let messagestring = messageInput.text!
        
        let realm = try! Realm()                      //將資料寫入Realm
        let pp = iteamTable(name: userstring,
                        content: messagestring,
                        timeStamp: timeTemp!)
            try! realm.write {
                realm.add(pp)
            }
            
            let nnail: String = ""           //輸入完清空欄位
            userInput.text = nnail
            messageInput.text = nnail
            
            fetchData()
            tabelview.reloadData()
        }

上面執行寫入realm但最後有一個fetchData()是將realm儲存的資料匯入struct的iteamTable

func fetchData() {
        iteamTabel = []
        let realm = try! Realm()
        let dogs = realm.objects(iteamTable.self)
            if dogs.count > 0 {
                for i in 0...dogs.count - 1 {
                    let item = IteamTable(name: dogs[i].name,
                                          content: dogs[i].content,
                                          timeStamp: dogs[i].timeStamp,
                                          uuid: dogs[i].uuid)
                    iteamTabel.append(item)
                }
            }
            print("file\(realm.configuration.fileURL!)")
        }

上一篇
Day8 留言板2
下一篇
Day10 留言板4
系列文
swift 新手路程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言