如果我在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)
}
}
发布于 2018-10-30 06:06:21
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)
}
}
只需从let friendcomparison = "yes"
中删除let
并在let friendcomparison = self?.friendcomparison
中使用[weak self]
https://stackoverflow.com/questions/53054329
复制相似问题