gpt4 book ai didi

ios - 条件绑定(bind)的初始化程序必须具有可选类型,而不是 'String' - ios - swift

转载 作者:行者123 更新时间:2023-11-28 14:31:03 25 4
gpt4 key购买 nike

这个错误的解决方案是什么?我尝试了所发现的,但无法解决我的问题。

错误在这里:

if var post = currentData.value as? [String : Any], let uid = Auth.auth().currentUser!.uid {

完整代码:

func incrementLikes(postId: String, onSucess: @escaping (Post) -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
let postRef = Api.Post.REF_POSTS.child(postId)
postRef.runTransactionBlock({ (currentData: MutableData) -> TransactionResult in
if var post = currentData.value as? [String : Any], let uid = Auth.auth().currentUser!.uid {
var likes: Dictionary<String, Bool>
likes = post["likes"] as? [String : Bool] ?? [:]
var likeCount = post["likeCount"] as? Int ?? 0
if let _ = likes[uid] {
likeCount -= 1
likes.removeValue(forKey: uid)
} else {
likeCount += 1
likes[uid] = true
}
post["likeCount"] = likeCount as AnyObject?
post["likes"] = likes as AnyObject?

currentData.value = post

return TransactionResult.success(withValue: currentData)
}
return TransactionResult.success(withValue: currentData)
}) { (error, committed, snapshot) in
if let error = error {
onError(error.localizedDescription)
}
if let dict = snapshot?.value as? [String: Any] {
let post = Post.transformPostPhoto(dict: dict, key: snapshot!.key)
onSucess(post)
}
}
}

enter image description here

我不知道如何直接添加图片。抱歉

最佳答案

因为 Auth.auth().currentUser!.uid 返回一个 String,而不是 String?。如果你想执行条件检查,你应该使用

if let uid = Auth.auth().currentUser?.uid {
//Logic here
}

如果你想将 Auth.auth().currentUser!.uid 的值分配给 uid,你应该在别处进行,而不是在 if 语句中

关于ios - 条件绑定(bind)的初始化程序必须具有可选类型,而不是 'String' - ios - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51262811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com