如果我在 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/