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 Swift Initializers

Kurt L
Kurt L
22,856 Points

Hi. Is there a reason not to put the getCurrentWeatherData() -> Void function in the Current class? Bad practice?

Hi. Is there a reason not to put the getCurrentWeatherData() -> Void function in the Current class? Bad practice?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Kurt,

The theory behind this is to create modular and reusable code that follows the D.R.Y principle which is:

  • Don't
  • Repeat
  • Yourself

Follow these principles and techniques allows us to create code that we can take and reuse without needing to rewrite it over and over which can become very cumbersome and tiring if we're doing it more than once or twice, technically we can put this method in our main view controller but this defeats the purpose of having reusable code.

Hope that helps.

Kurt L
Kurt L
22,856 Points

Hi Chris, Thanks for your reply. :-)

I think I should have been clearer in my question. What I meant to ask was, in the weather app, the getCurrentWeatherData method is in the main ViewController class. There is no repeated code this way, and it seems to be common practice to fetch JSON data in the main ViewController, and then pass those data to the data model struct ("Current"), to assign to stored properties. But if we move this method into the Current struct there shouldn't be repeated code this way either. We could just call that method in the ViewController using dot notation on an instance of "Current". Does it violate MVC in some way to have a method in a data model class/struct that connects to the server?

Chris Shaw
Chris Shaw
26,676 Points

Sorry for the confusion, I misread your initial post which explains my somewhat backwards reply, in context I was referring to ViewController instead of Current so it's pretty much the same difference but backwards.

Does it violate MVC in some way to have a method in a data model class/struct that connects to the server?

Personally I wouldn't store a method such as getCurrentWeatherData within a struct as they should be considered as models that contain only relevant data, instead I would use a static class that I can pick up and drop into any project without any reliance on the previous code, what I mean by a static class is the following.

class Weather {
  class func getCurrentData() -> NSData {
    return /* response from server */
  }
}

let response = Weather.getCurrentData()