Swift Realm 筆記

Mario tsai
Feb 14, 2022

快速入門

use Realm

let realm = try! Realm()

Make a Model

import Foundation

import RealmSwift

class LogData: Object{

@Persisted(primaryKey: true) var _id: ObjectId

@Persisted var date: String?

@Persisted dynamic var time: String?

convenience init(content: String){

self.init()

}

}

func addNewLog(_ time:String,_ date:String) -> LogData{

let newLog = LogData()

newLog.time = time

newLog.date = date

return newLog

}

func save() {

let newLog = self.addNewLog(selectedTime, didSelectDate)

try! realm.write{

realm.add(newLog)

}

print(newLog)

}

--

--

Mario tsai

Major in physical education. Football referee. And high interest in looking the world through the numbers.