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!