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

iOS

Scott Lind
Scott Lind
2,524 Points

Swift simple weather app with Pasan: error

Hello. In the video called "Using our JSON Data" with Pasan, he makes the app crash on purpose - because of a faulty URL. After that, he uses the

      if (error == nil) {
    let dataObject = NSData(contentsOfURL: location)
    let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary    
        }

In XCode a get the error: use of unresolved identifier 'error'.

Does any of you encounter the same problem? I've done exactly as Pasan (or at least I think I have) but it gives me this error. And yes: I did make the app crash :-)

Does anyone know the solution to my problem?

Thanks!

  • Scott
Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hi Scott.

Can you please paste a little bit more of your code? Like all the rows above that part?

ty

2 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hi.

There is a compiling error when you start the asynchronous task.

You wrote "erroe" instead on error. This is at the end of the constant downloadTask line.

Trying correcting that to see if it works and let me know because I haven't tested your code at all for now. ;)

Also, very important: I would NOT disclose the apiKey like you did. I would recommend that you do a apiKey reset on the forecast.io site and fill in the new key in your code.

;)

Scott Lind
Scott Lind
2,524 Points

How typical!

Thanks!

Scott Lind
Scott Lind
2,524 Points

Here's all of my code from the ViewController.swift-file:

```import UIKit

class ViewController: UIViewController {

private let apiKey = "4d4a05ae5922e2f93ab21d473a6cdc04"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    let forecastURL = NSURL(string: "57.046345,9.918729", relativeToURL: baseURL)

    let sharedSession = NSURLSession.sharedSession()
    let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: NSURL!, response: NSURLResponse!, erroe: NSError!) -> Void in


    let dataObject = NSData(contentsOfURL: location)
    let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary    

    let currentWeather = Current(weatherDictionary: weatherDictionary)
        println(currentWeather.currentTime!)



    })
    downloadTask.resume()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

} ```