Help us understand the problem. What is going on with this article?

Realm 使い方

More than 1 year has passed since last update.

コード作成

Item.swift
import RealmSwift
class Item: Object{
    // 品名
    dynamic var name: String? = nil
    // 金額
    dynamic var price = 0
    // 登録日時
    dynamic var created = Date()
}

1. 保存

ViewController.swift
        // 保存
        let realm = try! Realm()// デフォルトRealmを取得
        try! realm.write {
            realm.add(item)
        }

2. 読込

ViewController.swift
        // 読込
        let realm = try! Realm()// デフォルトRealmを取得
        self.itemList = realm.objects(Item.self).filter("price > 0").sorted(byProperty: "created", ascending: false)

hrichii
理系大学院生 機械学習始めました
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away