Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Carl Martin
12,088 PointsHow do I implement UIAlertController using Swift?
I'm trying to show UIAlerts instead of just printing errors to the console. I read Apple's documentation, but there isn't much info about how to implement this in Swift.
This is the basic syntax I could piece together from other sources:
let alertController = UIAlertController(title: "Title of Alert", message: "Type message here.", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
Whenever I implement this in the downloadJSONFromURL function in the NetworkOperation.swift file, I get the error Value of type 'NetworkOperation' has no member 'presentViewController'.
What did I do wrong?
1 Answer

Carl Martin
12,088 PointsI found the answer. The problem was that I was trying to present a view, while not being in the ViewController. The following code worked in the NetworkOperation.swift file from the Build a Weather App with Swift Course:
dispatch_async(dispatch_get_main_queue(), {
let HTTPAlert = UIAlertController(title: "GET Request not Successful", message: "HTTP Status Code: \(httpResponse.statusCode)", preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
print("User clicked OK")
}
HTTPAlert.addAction(OKAction)
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(HTTPAlert, animated: true, completion: nil)
})