如果在尝试从init外部访问变量时在if语句中设置了变量,则会收到错误消息“实例成员'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)
}
}
只需从
let
删除let friendcomparison = "yes"
并将
[weak self]
与let friendcomparison = self?.friendcomparison
一起使用关于ios - 如何在init Swift中在IF语句之外获取变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53054329/