Created
September 5, 2015 18:09
-
-
Save matt-lebl/48daa9a8ab4c97dd2230 to your computer and use it in GitHub Desktop.
Some help for you, u/sziehr!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Alamofire | |
import SwiftyJSON | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// We'll be using forecast.io, as I'm unfamiliar with Weather Underground. | |
// This is the api key you'd get from developer.forecast.io | |
let apiKey = "<YOUR API KEY HERE>" | |
// These are the coordinates for Alcatraz Island, in case you need hyper-local | |
// weather forecasting to aid in an escape. ;) | |
let coordinates = "37.826979,-122.422962" | |
// Here, we're building the URL to get our data out of our defined constants. | |
// Refer to the apidocs for more info. https://developer.forecast.io/docs/v2 | |
let forecastURL = "http://api.forecast.io/forecast/\(apiKey)/\(coordinates)/" | |
// For networking, we'll use Alamofire. https://github.com/Alamofire/Alamofire | |
// It provides a nice, clean networking interface, allowing you to focus on handling | |
// your data. | |
// | |
// We'll use the URL we made above, using .GET and handling the response data in the | |
// closure. | |
Alamofire.request(.GET, forecastURL).response { request, response, rawData, error in | |
// Checking to make sure there wasn't any networking error | |
if error == nil { | |
// Here, we'll use SwiftyJSON https://github.com/SwiftyJSON/SwiftyJSON | |
// It makes accessing JSON data super easy. | |
let data = JSON(data: rawData!) | |
// Now, we're checking if we can get the temperature from the returned data. | |
if let temperature = data["currently"]["temperature"].float { | |
// If yes, print it! | |
println(temperature) | |
} else { | |
// If no, say so and print all the data so we can investigate. | |
println("Error in retrieving current temperature") | |
println(data) | |
} | |
} else { | |
// If there does happen to be an error, print it out so we can have a look. | |
println(error!) | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment