前提
XcodeでFirestoreのデータを取得しようとしています。
ですが、実行するとLocationsViewModel.swiftのlet locations = model.storesに
'self' used in property access 'model' before all stored properties are initialized
とエラーが現れます。
プロジェクトとFirebaseは連携できていることは確認できています。
回答していただけると大変ありがたいです。
どうぞ、よろしくお願いいたします。
実現したいこと
エラーを解決し、Firestoreのデータをアプリに反映させる。
発生している問題・エラーメッセージ
LocationsViewModel.swift1行目のlet locations = model.stores
'self' used in property access 'model' before all stored properties are initialized
該当のソースコード
LocationsViewModel.swift
1class LocationsViewModel: ObservableObject { 2 @ObservedObject var model = FirebaseViewModel() 3 4 init() { 5 let locations = model.stores 6 self.locations = locations 7 self.mapLocation = locations.first! 8 self.updateMapRegion(location: locations.first!) 9 } 10}
Firestoreからデータを取得するコード
FirebaseViewModel.swift
1import SwiftUI 2import FirebaseFirestore 3import MapKit 4 5class FirebaseViewModel: ObservableObject { 6 @Published var stores = [Location]() 7 8 init() { 9 self.stores = stores 10 getData() 11 } 12 13 14 func getData() { 15 16 let db = Firestore.firestore() 17 18 db.collection("stores").getDocuments { snapshot, error in 19 if error == nil { 20 // No errors 21 if let snapshot = snapshot { 22 DispatchQueue.main.async { 23 self.stores = snapshot.documents.map { d in 24 return Location(name: d["name"] as? String ?? "", 25 category: d["category"] as? String ?? "", 26 link: d["url"] as? String ?? "", 27 description: d["description"] as? String ?? "", 28 coordinates: d["coordinates"] as! GeoPoint, 29 id: d.documentID) 30 } 31 } 32 } 33 } 34 else { 35 } 36 } 37 38 } 39} 40
### 補足情報(FW/ツールのバージョンなど) macOS バージョン12.4 1.1GHz 4コアIntel Core i5 Xcodeバージョン13.4.1

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/07 23:07
2022/09/08 06:16
退会済みユーザー
2022/09/08 06:21
2022/09/10 04:13
退会済みユーザー
2022/09/10 06:19