2017-05-09 40 views
0

我正在使用Alamofire調用Riot API,並且我想要顯示它調用的信息。我有get請求工作,我只是不知道如何鏈接到應用程序中的標籤。我已經包含了代碼的截圖!想要將API數據顯示到標籤(Swift,Alamofire)

這僅僅是一個簡單的應用程序,我創造!

func callAlamo(url: String){ 
    Alamofire.request(url).responseJSON(completionHandler: { 
    response in 
    self.pasrseData(JSONData: response.data!) 
    }) 
    } 

func parseData(JSONData: Data){ 
    do { 
     var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as? JSONStandard 
     print(readableJSON) 
} 

catch { 
    print(error) 
    } 
} 
+1

請向我們展示你的代碼。 –

+0

編輯原始文本,爲控制檯響應打開屏幕截圖 –

回答

0

您可以設置標籤的Text屬性在完成塊,基本上是:

func callAlamo(url: String){ 
    Alamofire.request(url).responseJSON(completionHandler: { 
     response in 

     // here we say get me a non optional data (otherwise don't do the if) 
     if let data = response.data { 
      // here we are saying if you can't get me a value (i.e. not nil) for: 
      // json (note the try? will give nil if there is an error) 
      // name, we get the name out of the json dictionary 
      // then go to the else block, where we exit the function 
      // Happy case where we values for json and name we now have non optional variables W00t 
      guard 
      let json = try? self.parseData(JSONData: data), 
      let name = json["name"] as? String 
       else { 
        print("name does not exist in json: \(json)") 
        return 
      } 
      // Time to set the label 
      self.name.text = name 
     } 
    }) 
} 

// Changed this to return JSON as a dictionary (it looks like what you printed was a dictionary) 
// I also changed this so it throws the error and doesn't deal with it. 
// It probably doesn't know what to do if it can't read json something 
// else should handle the error higher up the stack 

func parseData(JSONData: Data) throws -> [String: Any]? { 
    return try JSONSerialization.jsonObject(with: 
    JSONData, options: .mutableContainers) as? [String: Any] 
} 

注:這是未經測試的,如果你有問題,我會去一個測試的解決方案。

編輯:回答如何獲得另一個屬性。

我們得到的方式「名」是該塊的代碼:

guard 
let json = try? self.parseData(JSONData: data), 
let name = json["name"] as? String 
    else { 
     print("name does not exist in json: \(json)") 
     return 
} 

要獲得另一個屬性了,我們可以這樣做:

guard 
let json = try? self.parseData(JSONData: data), 
let name = json["name"] as? String, 
let summonerLevel = json["summonerLevel"] as? Int 

    else { 
     print("name does not exist in json: \(json)") 
     return 
} 

然後顯示summonerLevel我們一樣做與名稱(雖然我們有一個int不是一個字符串)

// Time to set the label 
self.name.text = name 
// (you will have to create this new label) 
self.summonerLevelLabel.text = "Level: \(summonerLevel)" 
+0

This works a treat!另一個問題是,json中有另外一個條目,如「name」,但它是「summonerLevel」,我怎麼去添加呢? –

+0

還有一個比Alamofire更好的序列化JSON的方法!如果你想,我會發送一個例子。 –

0

因爲沒有需要序列化來自Alamofire的已經做到了。由於我不知道你的JSON對象裏面有什麼,假設你得到一個年齡和名字的回報:

struct InfoModel { // this struct will decompose our JSON object from the response that we get 
    var age:Int 
    var name:String 
    init(json:Dictionary<String,Any>?) { 
     guard let dict = json, 
     let age = dict["age"] as? Int, 
     let name = dict["name"] as? String 
      else {fatalError() } 
     self.age = age 
     self.name = name 

    } 
} 

func parse(url: String, completion:@escaping (InfoModel)-> Void) { 
    Alamofire.request(url).responseJSON {response in 
     // get the JSON dictionary 
     if let JSON = response.result.value { 
      // no need to decompose your object since your struct does it inside of its initializer 
      completion(InfoModel(json: JSON as? Dictionary<String, Any>)) 
     } 
    } 
} 
// call this function anywhere 
parse(url: "") { (m:InfoModel) in 
    print("age= \(m.age), name= \(m.name)") 
    // now populate your label 
    labelOne.text = "\(m.age)" 
    labelTwo.text = name 
}