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 Build a Weather App with Swift Managing Complexity ForecastService

for Xcode 7 the syntax used won't work most of the time. println has been deprecated etc...

if let currentWeatherDictionary = jsonDictionary["currently"] as? [String: AnyObject] the compiler gives an error Cannot Subscript a value of type'[String: AnyObject]?' win an index of type 'String'

may need some revisions tell then i am Stuck.

Soon Keat Ng
Soon Keat Ng
4,149 Points

if let currentWeatherDictionary = jsonDictionary?["currently"] as? [String: AnyObject]?{ } i have this code with no error maybe u lost a question mark after the jsonDictionary?

5 Answers

Remove the question mark after as. You have updated you Xcode and it is using the new Swift 2 syntax. You might also have to use NSString instead of String. Ran into the same problem in a program I'm writing now.

Karl Metum
Karl Metum
3,447 Points

This works for me in XCode 7.0.1:

if let currentWeatherDictionary = jsonDictionary?["currently"] as? [String: AnyObject] {
    return CurrentWeather(weatherDictionary: currentWeatherDictionary)
}
else {
    print("JSON dictionary returned nil for 'currently' key")
    return nil
}
Mike Gomes
Mike Gomes
15,391 Points

Also, this line

let networkOperation = NetworkOperation(url: forecastURL)

is not working for me, the error is:

Use of unresolved identifier 'NetworkOperation'

Do you guys have the same issue?

Karl Metum
Karl Metum
3,447 Points

Make sure you have a file called NetworkOperation.swift inside your project with the following content:

import Foundation

class NetworkOperation {

    lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
    lazy var session: NSURLSession = NSURLSession(configuration: self.config)
    let queryURL: NSURL

    typealias JSONDictionaryCompletion = ([String: AnyObject]?) -> Void

    init(url: NSURL) {
        self.queryURL = url
    }

    func downloadJSONFromURL(completion: JSONDictionaryCompletion) {
        // Load URL
        let request = NSURLRequest(URL: queryURL)
        // Make URL request
        let dataTask = session.dataTaskWithRequest(request) {
            (let data, let response, let error) in

            // 1. Check HTTP response for successful GET request
            if let httpResponse = response as? NSHTTPURLResponse {
                switch(httpResponse.statusCode) {
                case 200:
                    // 2. Create JSON Object with data
                    do {
                        let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as! [String: AnyObject]
                        // Send data to closure
                        completion(jsonDictionary)
                    } catch {
                        print(error)
                    }

                default:
                    print("GET request not successful. HTTP status code: \(httpResponse.statusCode). Url: \(httpResponse.URL)")

                }
            } else {
                print("Error: Not a valid HTTP response")
            }

        }

        dataTask.resume()
    }
}
Mike Gomes
Mike Gomes
15,391 Points

Yey :) It's working now! Thank you very much Karl, you are awesome!

Karl Metum
Karl Metum
3,447 Points

My pleasure. I'm glad it worked :)

Scott Brown
Scott Brown
4,406 Points

I know this isn't my post but thank you Karl!!!!