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 (Retired) Data Modeling With Structures Cleaning Up Our Date

My code doesn't return anything

I got the code exactly as in the video, but it doesn't return me the currentTime. Can someone help me please? This is my Current.swift - file:

import Foundation

struct Current {

    var currentTime: String?
    var temperature: Int
    var humidity: Double
    var precipProbability: Double
    var summary: String
    var icon: String

    init(weatherDictionary: NSDictionary) {
        let currentWeather = weatherDictionary["currently"] as NSDictionary

        temperature = currentWeather["temperature"] as Int
        humidity = currentWeather["humidity"] as Double
        precipProbability = currentWeather["precipProbability"] as Double
        summary = currentWeather["summary"] as String
        icon = currentWeather["icon"] as String

        let currentTimeIntValue = currentWeather["time"] as Int
        currentTime = dateStringFromUnixTime(currentTimeIntValue)
    }

    func dateStringFromUnixTime(unixTime: Int) -> String {
        let timeInSeconds = NSTimeInterval(unixTime)
        let weatherDate = NSDate(timeIntervalSince1970: timeInSeconds)

        let dateFormatter = NSDateFormatter()
        dateFormatter.timeStyle = .ShortStyle

        return dateFormatter.stringFromDate(weatherDate)
    }
}

2 Answers

Hi Sander,

That all looks fine.

Where are you expecting the time to be returned? Is there a UI that goes with this at this stage?

Do you definitely have the JSON coming back from forecast.io?

Steve.

In the begin of the video i watched, i returned the temperature en that worked well. but now it doesn't return anything

i just restarted xcode for the 3rd time and now its finally working

Yes, that's right.

The time is obtained from the JSON data, manipulated to look like a familiar time, rather than a huge number and then output to the console using println(currentWeather.currentTime - is that in your code inside the if(error == nil) block?

Ah yes, the joys of restarting Xcode - I'm glad you got it fixed. It could have been a small netork glitch too.

Enjoy the rest of the app and have a look at the Android one as well. The two together make a great comparison.

Steve.