gpt4 book ai didi

ios - 如何在 init Swift 中获取 IF 语句之外的变量

转载 作者:行者123 更新时间:2023-11-30 11:03:05 26 4
gpt4 key购买 nike

如果我在 if 语句内设置变量,当我尝试访问 init 外部的变量时,我会收到错误“实例成员 'friendcomparison' 不能在类型 'User' 上使用”。任何帮助将不胜感激,谢谢。

let friendcomparison: String

func getfriendname() {
if frienddeeper == Auth.auth().currentUser?.uid {
let friendcomparison = "yes"
}
let link = URL.init(string: credentials["profilePicLink"]!)
URLSession.shared.dataTask(with: link!, completionHandler: { (data, response, error) in
if error == nil {
let profilePic = UIImage.init(data: data!)
let user = User.init(name: name, username: username, email: email, id: id, friendstatus: friendcomparison, token: token!, profilePic: profilePic!)
completion(user)
}
}

最佳答案

    func getfriendname() {
if frienddeeper == Auth.auth().currentUser?.uid {
friendcomparison = "yes"
}
let link = URL.init(string: credentials["profilePicLink"]!)
URLSession.shared.dataTask(with: link!, completionHandler: { [weak self] (data, response, error) in
if error == nil {
let profilePic = UIImage.init(data: data!)
let friendcomparison = self?.friendcomparison
let user = User.init(name: name, username: username, email: email, id: id, friendstatus: friendcomparison, token: token!, profilePic: profilePic!)
completion(user)
}
}

只需从 letfriendcomparison = "yes" 中删除 let

并使用[weak self]letfriendcomparison = self?.friendcomparison

关于ios - 如何在 init Swift 中获取 IF 语句之外的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53054329/

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