【问题标题】:Swift working with API "to fast"?Swift 使用 API “快速”?
【发布时间】:2014-10-22 14:00:12
【问题描述】:

我目前正在为一个小应用程序使用英雄联盟 API - 它要求我一个接一个地发送一些请求,但目前我遇到了问题。 首先是我的代码

import UIKit

var summonorname:String = ""

class ViewController: UIViewController {
    var SummonorID = ""


    @IBOutlet weak var Input: UITextField!
    @IBAction func SubmitBT(sender: AnyObject) {
        summonorname = Input.text
        println(summonorname)

        let urlpath_summonorID = "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/"+summonorname+"?api_key=12345-12345-12345-12345-12345"


        let url_summonorID = NSURL(string: urlpath_summonorID)
        let session_1 = NSURLSession.sharedSession()
        let task_1 = session_1.dataTaskWithURL(url_summonorID, completionHandler: {data,response, error -> Void in
            if (error != nil) {
                println(error)
            }else{
                let summonorID_JSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
                var ID_dict = summonorID_JSON["testsummonor"] as NSDictionary
                var SummonorID: AnyObject? = ID_dict["id"]
                println(SummonorID!)
            }
        })

        task_1.resume()

        let urlpath_summonorLeague = "https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/"+self.SummonorID+"?api_key= 12345-12345-12345-12345-12345"
        println(urlpath_summonorLeague)

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

第一个请求发送正常,我得到 JSON 并且可以解析它,但是

let urlpath_summonorLeague = "https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/"+self.SummonorID+"?api_key=12345-12345-12345-12345-12345"
        println(urlpath_summonorLeague)

这部分被快速发送,所以它仍然缺少“self.SummonorID”,并且“self.SummonorID”在like之后打印:

OUTPUT
testsummonor
https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/?api_key=12345-12345-12345-12345-12345
00000001

无论如何要解决这个问题,比如“让请求先返回”,然后进入下一步?

【问题讨论】:

    标签: json rest web swift request


    【解决方案1】:

    您只在代码中启动了一项任务。如果您想在 completion 任务之后完成某些事情,请将其放入 completion handler

    【讨论】:

    • 见下面的帖子,但这甚至不起作用(它给了我相同的输出)
    【解决方案2】:

    @蒙迪

    import UIKit
    
    var summonorname:String = ""
    
    class ViewController: UIViewController {
        var SummonorID = ""
    
    
        @IBOutlet weak var Input: UITextField!
        @IBAction func SubmitBT(sender: AnyObject) {
            summonorname = Input.text
            println(summonorname)
    
            let urlpath_summonorID = "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/"+summonorname+"?api_key=12345-12345-12345-12345-12345"
    
    
            let url_summonorID = NSURL(string: urlpath_summonorID)
            let session_1 = NSURLSession.sharedSession()
            let task_1 = session_1.dataTaskWithURL(url_summonorID, completionHandler: {data,response, error -> Void in
                if (error != nil) {
                    println(error)
                }else{
                    let summonorID_JSON = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
                    var ID_dict = summonorID_JSON["testsummonor"] as NSDictionary
                    var SummonorID: AnyObject? = ID_dict["id"]
                    println(SummonorID!)
    let urlpath_summonorLeague = "https://euw.api.pvp.net/api/lol/euw/v2.5/league/by-summoner/"+self.SummonorID+"?api_key= 12345-12345-12345-12345-12345"
            println(urlpath_summonorLeague)
    
                }
            })
    
            task_1.resume()
    
    
        }
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 2017-09-12
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多