Where to go after the basics?

Hello everyone!

Iā€™ve been developing in Swift for about a year. Iā€™ve learned a lot trough the tutorials here and just by doing my own researches but I still believe I have a lot more to improve as a developer. Iā€™ve done many things but Iā€™ve never had someone to actually check my coding and discuss it with me, or someone to discuss about the daily challenge, I just kind of did the way it ā€œworkedā€. Do you guys have any suggestions for me so I can validate the work Iā€™ve been doing? I mean itā€™s working but maybe not in the best way.

Thanks !!

What do you think about bringing your examples here, to discuss? It seems like a pretty good format to me.

OK, sounds good to meā€¦Iā€™ll use this post for the first question since I canā€™t create a topic at the General Discussion areaā€¦
So, I have an app that Iā€™m buildingā€¦In this app I receive one JSON that I will pretty much populate my whole app with information from it. Iā€™m using Alamofire for the requests and SwiftyJSON to manipulate the data. Iā€™ve generated the classes trough JSONExport, and after the http request I just create the classes that Iā€™ve generated with SwiftyJSON, set them in the NSUserDefaults and then Iā€™ve created a singleton of my main class that would try to get the information from NSUserDefaults and return it if I donā€™t have internet connection or if occurred any error.
1-Is it ok to generate classes trough this app?
2-Is it ok to set in the NSUserDefault the whole JSON that I received in the first request? I know that NSUserDefault is usually used for things like User Preference but is it worth usign SQLite? Core Data? Wouldnā€™t that make it a lot more complex?
3-Does it make sense to create a singleton of the main class that gets the information from another singleton ? How should I do that? (if Iā€™m not mistaken, NSUserDefault is a singleton that persists right? )
4-Iā€™ve made my singleton from the main class an optional so if I canā€™t get the information from NSUserDefault I just return nil and inside the ViewControllers where I use it i just do a
ā€œif let variable = singletonFromMainClass
{
self.variable = variable
}ā€

Is this the best approach?

thanks once again.

  1. I donā€™t know, Iā€™ve not used JSONExport, but if itā€™s taking a JSON file and making a struct template or something then it canā€™t hurt to give you a starting point.
  2. You can put a data structure into NSUserDefaults, but personally I would probably try to save it into a file in a data directory.
  3. NSUserDefaults is a class; its ā€˜standardUserDefaultsā€™ item is usually the one used, and is usually thought of as a singleton, but you can have more than one. I would avoid using a singleton where possible (unless itā€™s for something like the UIApplication delegate, which there really should be only one of). There are other ways to structure your app to pass data around; limiting the number of types which can access your information reduces the number of places you have to check if something accesses it incorrectly.
  4. Your code example implies that self.variable is an optional. You might as well just type self.variable = singletonFromMainClass, if thereā€™s no other need for your variable variable.

As your third question leads to the bigger questions more directly: after the basics, I would look into app architecture and design patterns. Improving your appā€™s APIs through its data encapsulation can make it more predictable and less error-prone - especially if you also write unit tests to prove it all works.

Thanks for the reply.

2- What you think about keeping it in cache? Where I work my supervisor told me to do so, and Iā€™ve been looking into it and found this reference which looks like a very good approach for the purpose.

3- Any references on how to pass data around my app? Letā€™s say I have a structure like:
School ā†’ Course ā†’ Subjectā€¦ Inside the JSON I receive an array of Schools with their Courses and inside the Courses the Subjects of that Courses. In some ViewControllers Iā€™ll use information about the School, and in the next VC info about the Course etcā€¦ You would save those informations in separate files ? I canā€™t visualize another way to limit the access of types with that structure.

4- self.variable = singletonFromMainClass.array ?? []
My singleton is an optional but my variable isnā€™t ā€¦Would that be a better approach?

  1. Iā€™ve not worked much with the cache idea, I canā€™t help you much there. Iā€™d assumed you meant saving the data for more permanent storage. ā€¦ I guess my response to this would just be to try it. At worst youā€™ll know not to do that again.

  2. The straightforward way to pass data around would be to override your view controllersā€™ prepareForSegueWithIdentifier functions.
    The ā€˜going past the basicsā€™ study would be to try out some of the architecture patterns, which (to summarise) would give you an idea of how to break your projectā€™s types up into smaller types with smaller responsibilities - ideally, one responsibility per type - such that some types are responsible for maintaining a consistent data model, some types are responsible for what your app actually does, and others are responsible for displaying the state of the data model to the user. The best known is MVC (Model-View-Controller), but there are others; MVVM (Model-View-ViewModel) is one, VIPER (View-Interactor-Presenter-Entity-Router) is another, there are more. The RW site has some tutorials into design patterns and I believe MVC and MVVM are covered somewhere. There are articles on the objc.io website (issue 13 of its magazine, I think) which go into MVVM and VIPER. These may give you some ideas on the ā€˜big pictureā€™ of how to structure your app.

  3. I donā€™t know your app, but if assigning an empty array to self.variable makes more sense than a value of nil, use it.

Awesome! Great help, thank you very much!