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.
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.
- 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.
- You can put a data structure into NSUserDefaults, but personally I would probably try to save it into a file in a data directory.
- 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.
- 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
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?
Awesome! Great help, thank you very much!