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

Enhance a Weather App with Table Views: The current time is not updated

currentTime: String?
 if let time = weatherDictionary["time"] as? Double {
        currentTime = timeStringFromUnixTime(time)
   } else {
        currentTime = nil
 }

    func timeStringFromUnixTime(unixTime: Double) -> String {
        let date = NSDate(timeIntervalSince1970: unixTime)

        // 12 hour format
        dateFormatter.dateFormat = "hh:mm a"
        return dateFormatter.stringFromDate(date)
    }

I want to show the current time an the weather app, but the time updates only every hour (10:01, 11:01, etc) and not every minute. Can someone help me?

Hm, timeStringFromUnixTime works well here, provided that your NSDateFormatter is something like let dateFormatter = NSDateFormatter(). My guess is that time returns a wrong value? Can you test that? On the other hand, maybe you only get weather for every other hour?

Hello :

Ok so to understand this better.. You want to retrieve weather information from forecast.io along with the current time the weather was retrieved ? How are you implementing this ? will this refresh automatically ? Please give me a description of your idea and what you want as final result, perhaps I may help.

In my weather app, I want to show the current time that was retrieved from Forecast.io API, after I pull to refresh. Do you understand me Jhoan?

Hello:

I have not had time to sit and look into this, but my first option of debugging I would look into the function timeStringFromUnixTime(unixTime: ){}.

When retrieving the time from JSON, and comparing it to timeIntervalSince1970 will give you a large number, perhaps the type Double is not what you are looking as a parameter ?

That would be my first check.

    func timeStringFromUnixTime(unixTime: //Type ) -> String {
        let date = NSDate(timeIntervalSince1970: unixTime)

        // 12 hour format
        dateFormatter.dateFormat = "hh:mm a"
        return dateFormatter.stringFromDate(date)
    }

2 Answers

Hi Aleks,

If you're pulling back the data from forecast.io then there are four sets of time-based data. There are daily forecasts, hourly forecasts and forecasts by the minute; then there's the current forecast, updated each second. I think there are 8 future days individually set out, the next 24 hours individually and the next hour is set out by the minute.

The time that you are pulling from JSON isn't related to the current time, unless you're using currently data set - it is the time associated with the data chunk you have parsed. If you want a current time & forecast, use the currently piece of data and pull back time from that.

I've got some working code for that if you think it would help.

Steve.

Thank you for your response! I am using "currently" data set to retrieve the "current time". I appreciate it if you help me with that working code! :)

Hi Aleks,

Can you post the part of your code that accesses the "currently" JSON and populates the label concerned?

I checked the JSON page and it updates every second. Very odd it isn't working for you!

My code is on Github - the link is here (that's got location capability added too).

Post your code and have a look at my version. Let me know how you get on, too.

Steve.

if let currentWeatherDictionary = weatherDictionary?["currently"] as? [String: AnyObject] {
            currentWeather = CurrentWeather(weatherDictionary: currentWeatherDictionary)
        } 

(The part of the code that get "current" data from Forecast.io)

I only just noticed this is in Swift. My example is in Java. Sorry!

Let me have a look later to see if I can help with you code.

Apologies again.

Steve.

Aleks Disho - Can you post the bit of code where you access the time data, please?

Steve.

if let time = weatherDictionary["time"] as? Double {
            currentTime = timeStringFromUnixTime(time)
        } else {
            currentTime = nil
        }

(This is how I get the time.) On the JSON of Forecast.io the time is updated each second, but on my label it is updated not each second...

Jhoan Arango - you're well versed with Swift. Can you help out here, please?

Steve.