我从视频课程中编写了一个应用程序。我想修改它。我的任务是用户发表评论(以图像的形式)并将其传递给字典以将图像上传到 firebase 存储
字典
struct Tweet: Identifiable {
let id: String
let username: String
let profileImageUrl: String
let fullname: String
let caption: String
let retweet: UIImage?
let likes: Int
let uid: String
let timestamp: Timestamp
init(dictionary: [String:Any]) {
self.id = dictionary["id"] as? String ?? ""
self.username = dictionary["username"] as? String ?? ""
self.profileImageUrl = dictionary["profileImageUrl"] as? String ?? ""
self.fullname = dictionary["fullname"] as? String ?? ""
self.caption = dictionary["caption"] as? String ?? ""
self.retweet = dictionary["retweetimage"] as? ???????
self.likes = dictionary["likes"] as? Int ?? 0
self.uid = dictionary["uid"] as? String ?? ""
self.timestamp = dictionary["timestamp"] as? Timestamp ?? Timestamp(date: Date())
}
上传功能
func uploadRetweet(retweet: UIImage) {
guard let user = AuthViewModel.shared.user else { return }
let docRef = COLLECTION_RETWEETS.document()
let data: [String: Any] = ["uid": user.id,
"retweetimage": retweet,
"fullname":user.fullname,
"timestamp": Timestamp(date: Date()),
"username": user.username,
"profileImageUrl": user.profileImageUrl,
"likes": 0,
"id": docRef.documentID]
docRef.setData(data) { _ in
self.isPresented = false
}
}
调用函数
Button(action: {
print("DEBUG: HERE WILL BE RETWEET UPLOAD")
viewModel.uploadRetweet(retweet: ?)
}, label: