2

I've been trying to display certain JSON data to the storyboard but for some reason have been unable. The part of it that works, is the the var name, which is a string, and it doesn't have to be converted so it just works. The part that I am having an issue with is trying to convert two Int64's to strings, but I have them listed as AnyObjects. It's really confusing me, but here is the issue in code:

The program runs fine, but it doesn't display any information for profileIconId and 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)

  }
}

The code that processes and displays everything is in the connectionDidFinishLoading function at the very bottom of the code.

1
  • Can you post the JSON you're parsing Commented Jul 1, 2015 at 1:22

1 Answer 1

1

Here's a refactor of your connectionDidFinishLoading(_:) method that properly unwraps the values using optional bindings.

You might also consider using NSNumberFormatter instead of "\()".

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)
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.