Swift Realm 筆記


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{







Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mario tsai

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