【问题标题】:Swift after json parsing variables are assigned to their initial valuesswift json解析后变量赋值给它们的初始值
【发布时间】:2020-07-22 03:16:16
【问题描述】:

我是 swift 新手,如果这是一个愚蠢的问题,我很抱歉 我正在尝试扩展我在 macOS 开发方面的知识,并且正在尝试新事物 我正在从 url 解析一个 json 文件 它在 do{}catch{} 括号中工作正常但是,我想使用我从程序其他部分的 json 数据中获得的内容。

我创建了一些变量来存储值。

但是,一旦 do{}catch{} 执行完成,它们就会恢复到初始值 我如何存储我得到的值

@IBAction func buttonPressed(_ sender: Any) {
        var summonerNameGlobal: String = ""
        var summonerIdGlobal: String = ""
        var summonerPuuidGlobal: String = ""
        var summonerAccountIdGlobal: String = ""
        let jsonString = "https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/john?api_key=\(apiKey)"
        guard let url = URL(string: jsonString) else {return}
        URLSession.shared.dataTask(with: url) { (data, response, err) in
            guard let data = data else {return}
            DispatchQueue.main.async {
                do {
                    let summoner = try JSONDecoder().decode(SummonerInfo.self, from: data)
                    self.summonerIdLabel.stringValue = summoner.id
                    summonerNameGlobal = summoner.name
                    summonerIdGlobal = summoner.id
                    summonerAccountIdGlobal = summoner.accountId
                    summonerPuuidGlobal = summoner.puuid
                } catch {
                    print(error)
                }
            }
        }.resume()
        print(summonerNameGlobal)
        print(summonerPuuidGlobal)
        print(summonerIdGlobal)
        print(summonerAccountIdGlobal)
    }

【问题讨论】:

    标签: swift cocoa


    【解决方案1】:

    它们不会再次默认,但您在设置它们之前会检查它们...因为async function 需要一些时间才能从服务器获得响应,但您的 print 语句运行 immediately
    您可以做的是在设置值后检查它们

    func callApi(completion: @escaping (SummonerInfo?)->Void){
           
            let jsonString = "https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/john?api_key=\(apiKey)"
            guard let url = URL(string: jsonString) else {return}
            URLSession.shared.dataTask(with: url) { (data, response, err) in
                guard let data = data else {return}
                DispatchQueue.main.async {
                    do {
                        let summoner = try JSONDecoder().decode(SummonerInfo.self, from: data)
                       completion(summoner)
                    } catch {
                        completion(nil)
                        print(error)
                    }
                }
            }.resume()
        }
    
        @IBAction func buttonPressed(_ sender: Any) {
           
            callApi { [weak self] info  in
                
                if let getInfo = info {
                print(getInfo.name)
                print(getInfo.id)
                print(getInfo.accountId)
                print(getInfo.puuid)
                } else  {
                    print("data is nil")
                }
                
            }
            
          
        }
    

    【讨论】:

    • 非常感谢您的帮助
    猜你喜欢
    • 2019-01-26
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多