ButtonMasterBot
ButtonMasterBot

Reputation: 317

Using NSURLConnection With Swift

I am using the following code to get data from an API:

typealias JSONdic = [String: AnyObject]

if let json = json as? JSONdic, history = json["history"] as? JSONdic, hour = history["hour"] as? String {
println(hour)
}

However, Xcode tells me that "json" is not a recognized identifier. I believe this can be solved with NSURLConnection, but I have no idea how to use that. Can anyone provide any examples of this protocol in use?

Upvotes: 0

Views: 35

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 186984

You're declaring a variable by setting it to itself, which doesn't make any sense. In order to use a variable on the right hand side of an assignment, it needs to have already been declared. So let's give json a value outside of the casting statements and it works fine.

typealias JSONdic = [String: AnyObject]

let json: AnyObject = ["greeting": "Hello"]
if let json = json as? JSONdic, history = json["history"] as? JSONdic, hour = history["hour"] as? String {
    println(hour)
}

Upvotes: 1

Related Questions