简体   繁体   English

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

[英]How to get variable outside of IF statement in init Swift

If I set the variable inside the if statement when I try to access the variable outside on the init I get the error "Instance member 'friendcomparison' cannot be used on type 'User'". 如果我尝试在init外部访问变量时在if语句中设置变量,则会收到错误消息“实例成员'friendcomparison'不能用于'User'类型”。 Any help would be appreciated, thank you. 任何帮助,将不胜感激,谢谢。

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)
                            }
    }

Just remove let from let friendcomparison = "yes" 只需从let friendcomparison = "yes"删除let

and use [weak self] with let friendcomparison = self?.friendcomparison 并使用let friendcomparison = self?.friendcomparison [weak self]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM