留言板我們先坐輸入儲存近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!)")
}