|
I have been going through the projects from Hacking with Swift and I feel like my ViewController is getting more and more cluttered with code, I have no structure whatsoever. So I wanted to ask for an advice, where could I learn how to structure my code with MVC as the architecture for example? Is that something I could get from Paul's Design Patterns book? I looked at the first 24 pages, but it seemed to have only code snippets and not a real project. |
|
Paul's Swift on Sundays videos have some nice refactoring going on. That's a good start! https://www.youtube.com/results?search_query=swift+on+sundays |
|
This also depends on what type of your code is in the view controller. You have have for example a lot of lines that setup buttons to look in a particular way, then you can create custom If you have some functionality that deals with models, this should be on the model class. Like definitions of For other functionality that is not truly specific for the current view controller you can create It is not uncommon to have utils or helper classes that use the singleton pattern to move generic functionality there. For yet other things (like autosizing table view header/footer, generic setup) it may make sense to create base view controller and move it there. |
|
If you want, feel free to share your source code, I will check it out and suggest how to clean up the controller. |
|
I don't know how I could reply to each of you, so I am just gonna do it like this. @Gakkienl Thanks for the link, I'll check it out! @nemecek-filip That is actually really nice and more than I was expecting. I never had anyone review my code, so huge thanks in advance! Here is the ViewController of a small app I built alongside Hacking with Swift, it's a simple Todo App that saves and reads user input to and from a file.
|
|
Cool, thanks for sharing!
These methods are really problematic because The file manipulation could easily be extracted into something like The |
|
Thanks a lot for your feedback, I will be looking into the things you mentioned. About the cellForRowAt method though, I wonder how I would do this since ViewDidLoad does not run everytime the tableView gets reloaded, I am assuming here. So if the user wants to insert a new item to the To do list and the cells are being "filled" with the data set in viewDidLoad, the user would not be able to see his current addition now, but only after the app would restart because ViewDidLoad would read again the data from the file and the newly created item would then be there. I guess the best way to go here would be to update/create a single row instead of creating all the rows again after each insertion of a new item. This is the problem you meant, right? |
|
No, the way you have it is correct. |
|
Thank you all for your help. I figured out for myself that I should call tableView.insertRows(..) instead of tableView.reloadData(), because the later seems to reload all the cells while the first only calls cellsForRowAt for new the cell which I need to show. Since my initial question has been answered already I will be marking this as solved. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.