3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SwiftUI + MVVM + Realm で iOS アプリを開発

Last updated at Posted at 2022-03-05

内容

  • SwiftUI を利用したアプリを開発
  • アーキテクチャは MVVM を利用
  • データの永続化に Realm を使用

開発したアプリ

メモアプリ

開発環境

ハードウエア

項目
PC MacBook Air(M1,2020) メモリ:16GB ストレージ:1TB
実機 iPhoneSE(2nd Generation 128GB iOS 15.3.1)
PCと実機を接続する USB-C Digital AV Multiportアダプタ

ソフトウエア

項目
言語 Swift 5.5.2
IDE Xcode Ver 13.2.1
DB Realm
DB管理 Realm Studio
バージョン管理 GitHub

※ Realm は GitHub の Swift Package Manager を利用して導入

コード (一部)

Model
import Foundation
import RealmSwift

class Memo: Object,Identifiable {
    @objc dynamic var text = ""
    @objc dynamic var postedDate = Date()
}

extension Memo {
    private static var config = Realm.Configuration(schemaVersion: 1)
    private static var realm = try! Realm(configuration: config)
    
    static func findAll() -> Results<Memo> {
        realm.objects(Memo.self)
    }
    
    static func add(_ memo: Memo) {
        try! realm.write {
            realm.add(memo)
        }
    }
    
    static func delete(_ memo: Memo) {
        try! realm.write {
            realm.delete(memo)
        }
    }
    
    static func delete(_ memos: [Memo]) {
        try! realm.write {
            realm.delete(memos)
        }
    }
}
ViewModel
import Foundation

class MemoViewModel: ObservableObject {
    @Published var memos: [Memo] = Array(Memo.findAll())
    
    func add(text: String){
        guard !text.isEmpty else { return }
        let memo = Memo()
        memo.text = text
        self.memos.append(memo)
        Memo.add(memo)
    }
}

View
import SwiftUI
import RealmSwift

struct ContentView: View {
    @EnvironmentObject var memoVM: MemoViewModel
    @State var enteredText: String = ""
    
    var body: some View {
        var memos = memoVM.memos.sorted { $01.postedDate < $1.postedDate }
        
        NavigationView {
            VStack {
                HStack {
                    TextField("Enter Text", text: $enteredText)
                        .padding(7)
                        .border(.gray)
                        .cornerRadius(5)
                    
                    Button(action: {
                        addMemo(text: enteredText)
                    },label: {
                        Text("Add")
                            .bold()
                    }
                    )
                        .buttonStyle(.borderedProminent)
                }
                .padding()
                
                List(memos){ memo in
                    VStack(alignment: .leading){
                        Text(memo.text)
                        Text((memo.postedDate.description))
                            .font(.subheadline)
                            .foregroundColor(.gray)
                    }
                }
                .navigationTitle("Memo")
            }
            
        }
    }
    
    private func addMemo(text: String){
        memoVM.add(text: text)
    }
}

struct ContentView_Previews: PreviewProvider {
    
    static var previews: some View {
        ContentView()
            .environmentObject(MemoViewModel())
    }
}

備考

Realm Studio を利用するとデータを確認可能

参考資料

3
6
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
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?