Reputation: 6608
I currently stuck at get my JSON Parse into a string, here is my Code
var SummName = "xxxxx"
var APIKEY = "xxxx-xxx-43d5-8647-xxxx"
let urlPath0 = "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/"+SummName+"?api_key="+APIKEY
let url0 = NSURL(string: urlPath0)
let session0 = NSURLSession.sharedSession()
let task0 = session0.dataTaskWithURL(url0!, 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 SummonerID = summonorID_JSON["smirknaitia"] as NSDictionary
let SummID = SummonerID["id"]! as NSString
println(SummID)
let urlPath1 = "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/"+SummID+"?api_key="+APIKEY
}
})
task0.resume()
It always fails by unwrapping the Value of SummonerID["ID"] into a string
Upvotes: 0
Views: 475
Reputation: 6546
There are two ways let SummID = SummonerID["id"]! as NSString
can fail:
SummonerID
dictionary doesn't have a value for key "id", so it fails when you try to force-unwrapSummonerID["id"]!
is not of type NSString
(or type that can convert to it, like String
) so it fails when you try to force conversion.We can't really tell which one it is exactly without seeing the input you have or exact error messages.
A side note: it's not in Swift style to start variable names with uppercase characters. It's best to use camelCase for variables, functions and methods; use UpperCamelCase for classes, structs, enums and enum cases.
Upvotes: 1