2015-07-01 35 views
2

我一直在試圖向故事板顯示某些JSON數據,但出於某種原因一直無法使用。它的工作部分是var名稱,它是一個字符串,並且不需要轉換,因此它可以正常工作。我遇到問題的部分是試圖將兩個Int64轉換爲字符串,但我將它們列爲AnyObjects。這真的讓我困惑,但這是代碼問題:無法顯示JSON數據的一部分

該程序運行良好,但它不顯示任何信息profileIconId和summonerLevel。

import UIKit 

class ViewController: UIViewController, NSURLConnectionDelegate { 

lazy var data = NSMutableData() 

@IBOutlet weak var searchField: UITextField! 

@IBOutlet weak var name: UILabel! 

@IBOutlet weak var summonerLevel: UILabel! 

@IBOutlet weak var profileIconId: UILabel! 

@IBAction func enterButton(sender: AnyObject) { 

    startConnection() 

} 

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

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

} 

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

func startConnection(){ 
    let urlPath: String = "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/soon2challenger?api_key=(removed my private api key for obvious reasons)" 
    var url: NSURL = NSURL(string: urlPath)! 
    var request: NSURLRequest = NSURLRequest(URL: url) 
    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)! 
    connection.start() 
} 

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){ 
    self.data.appendData(data) 
} 

func buttonAction(sender: UIButton!){ 
    startConnection() 
} 


func connectionDidFinishLoading(connection: NSURLConnection!) { 
    var err: NSError 
    // throwing an error on the line below (can't figure out where the error message is) 
    var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary 

    let include: AnyObject = jsonResult.objectForKey(searchField.text)! 

    var name1: AnyObject = include.objectForKey("name")! 
    var summLevel: AnyObject = include.objectForKey("summonerLevel")! 
    var profIconId: AnyObject = include.objectForKey("profileIconId")! 

    name.text = name1 as? String 
    profileIconId.text = profIconId as? String 
    summonerLevel.text = summLevel as? String 

    println(name1) 
    println(summLevel) 
    println(profIconId) 

    } 
} 

,其處理並顯示一切的代碼在代碼的最底部的connectionDidFinishLoading功能。

+0

你可以發表你正在解析的JSON – Mark

回答

1

下面是您的connectionDidFinishLoading(_:)方法的重構,該方法使用可選綁定正確展開值。

您也可以考慮使用NSNumberFormatter而不是"\()"

func connectionDidFinishLoading(connection: NSURLConnection) { 

    var err: NSError? 

    if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary, 
     let include = jsonResult.objectForKey(searchField.text) as? NSDictionary { 

     if let name1 = include[ "name" ] as? String { 
      name.text = name1 
      println(name1) 
     } 

     if let summLevel = include[ "summonerLevel" ] as? NSNumber { 
      summonerLevel.text = "\(summLevel.integerValue)" 
      println(summLevel) 

     } 
     if let profIconId = include[ "profileIconId" ] as? NSNumber { 
      profileIconId.text = "\(profIconId.integerValue)" 
      println(profIconId) 
     } 
    } 
}