0
votes

I'm new to programming for ios so my question may be silly ;-) I'm trying to parse some JSON data. My code look's like:

var task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in

        if(error != nil){
            println("error")
        }else{
            var error: NSError?
            var wetherData = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSMutableDictionary

            var wd = wetherData["current_observation"] as NSMutableDictionary

            var xd = wd["temp_c"]! as String

            println(xd);

        }
    })

I want xd variable to be a string but while casting i have an error

enter image description here

println(wd["temp_c"]) returns "Optional(4)"

wd["temp_c"] look's like this

enter image description here

wd["temp_c"] is long with value of 4, so while printing a have Optional(4) - miru87
wd["temp_c"] is returning optional(4) you can probably chain a toInt() i.e. var xd = wd["temp_c"]?.toString() this will make xd: String? - JMFR
@JMFR 'AnyObject' does not have a member named 'toString()' - miru87
My suggestion is to use a temporary variable to store the "4" into then convert it into a string using the above String() constructor. var temp = wd["temp_c"]! as Int; var xd = String(temp) - JMFR
I found a problem, wd["temp_c"] is NSNumber, not long ;-) Thanks for helping me. This code works wd["temp_c"]!.stringValue - miru87