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){




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{







Mario tsai

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