我正在嘗試向我的服務結構添加一個新欄位(主題標簽)并在 Xcode 中出現錯誤。“在范圍內找不到'主題標簽'”是我的代碼。
import SwiftUI
import Firebase
import FirebaseFirestoreSwift
struct FeedTry: Identifiable, Decodable {
@DocumentID var id: String?
let caption: String
let myhashtags: String //Added this new field
let timestamp: Timestamp
let uid: String
var likes: Int
var saves: Int
var user: User?
var didLike: Bool? = false
var didSave: Bool? = false
}
以下是我的服務視圖
struct UploadTryService {
func uploadTry(caption: String, completion: @escaping(Bool) -> Void) {
guard let uid = Auth.auth().currentUser?.uid else { return }
let data = ["uid": uid,
"caption": caption,
"hashtags": myhashtags, //getting the error here
"likes": 0,
"saves": 0,
"timestamp": Timestamp(date: Date())
] as [String : Any]
Firestore.firestore().collection("try").document().setData(data) { error in
if let error = error {
print("DEBUG: Failed to upload try with error: \(error.localizedDescription)")
completion(false)
return
}
completion(true)
}
}
}
在網上和檔案中檢查,我還沒有找到修復它的方法。
uj5u.com熱心網友回復:
就像您對 所做的那樣caption,您可以將一個引數傳遞給uploadTry一個值為myhashtags:
struct UploadTryService {
func uploadTry(caption: String, myhashtags: String, completion: @escaping(Bool) -> Void) { //<-- Here
guard let uid = Auth.auth().currentUser?.uid else { return }
let data = ["uid": uid,
"caption": caption,
"myhashtags": myhashtags, //getting the error here
"likes": 0,
"saves": 0,
"timestamp": Timestamp(date: Date())
] as [String : Any]
Firestore.firestore().collection("try").document().setData(data) { error in
if let error = error {
print("DEBUG: Failed to upload try with error: \(error.localizedDescription)")
completion(false)
return
}
completion(true)
}
}
}
這意味著在您的方解石中,您還需要包含它:
uploadTry(caption: "Caption", myhashtags: "Hashtags here", ...)
另一種選擇(取決于您要查找的內容)是只傳入一個空的String:
let data = ["uid": uid,
"caption": caption,
"myhashtags": "",
"likes": 0,
"saves": 0,
"timestamp": Timestamp(date: Date())
請注意,在這兩種情況下,我都將字典鍵更改為,myhashtags因為這是您在模型中命名的。您可以選擇任一鍵——只要確保它們匹配即可。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446135.html
下一篇:我需要了解這些C指標的行為
