0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Swiftで、日誌アプリを作り始めた

Posted at

昨日から、ユーザークラスと日誌用のクラスを用意して、日誌アプリを作り始めた。
Realmの導入は、何度も試みてはリレーショナルで躓いてきたが、今回はやっとうまくいった。

class User: Object {
    @Persisted(primaryKey: true) var id: String = UUID().uuidString
    @Persisted var name: String = ""
    @Persisted var email: String = ""
    @Persisted var joinDate: Date = Date()
    @Persisted var preferences: String = "{}" // JSON文字列として保存
    @Persisted var dailyRecords: List<DailyRecord>

    override static func primaryKey() -> String? {
        return "id" // 一意のIDを指定
    }
}
}
class DailyRecord: Object {
    @Persisted(primaryKey: true) var id: ObjectId // 一意のID
    @Persisted var date: Date                    // 日付
    @Persisted var score: Int                    // スコア
    @Persisted var memo: String                  // メモ
    @Persisted var emotionalType: String         // 感情タイプ
    @Persisted var allowance: Double // お小遣い
    @Persisted var expense: Double // 出費
    let users = LinkingObjects(fromType: User.self, property: "dailyRecords") // Userへのリンクを保持
    // EmotionalType を String 型で保存するための計算プロパティ
     var emotionalTypeEnum: EmotionalType {
         get {
             EmotionalType(rawValue: emotionalType) ?? .happy
         }
         set {
             emotionalType = newValue.rawValue
         }
     }

    override static func primaryKey() -> String? {
        return "id" // 一意のIDを指定
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?