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
Simen Anthonsen
1,097 PointsOrganise code in Xcode
How should i organise my code in my Xcode project. Is there some simple guidelines? In my app I have got like 100 IBOutlets, is there for example a way to store them in a single swift file instead on the ViewController. Or doesn´t it matter that the viewController takes care of everything?
2 Answers
James Hoffman
6,354 PointsGreat question. I think this is actually one of the most important points for creating readable and maintainable code. You can create your own coding guidelines. I use the following sections for IBOutlets, Model Objects, & Local Variables respectively:
// View Objects (from top to bottom, left to right, and bottom layer to top layer)
// Model Objects
// Locals
Naming your variables intuitively and consistently also helps when you have so many IBOutlets so create a convention for that.
It is also very useful to mark the sections of code as below. The "MARK" creates a section title and the "-" puts a line between the sections. These marks can be seen by clicking on the file name directly above the source editor (e.g. clicking on MasterViewController when viewing MasterViewController.swift).
// MARK: - Initialization Functions
(section includes viewDidLoad(), configureView(), and didReceiveMemoryWarning() at a minimum)
(configureView() should be called in viewDidLoad() and contain all view formatting and setup)
// MARK: - Delegate Functions
// MARK: - Touch Event Functions
It's also useful to organize your files in the project navigator to fit the MVC model. I organize mine as VCM (Main.storyboard at the top, then Controllers (and files they use, e.g. customCell.swift), and finally Data Models) and then a folder for Supporting Files.
I'm pretty sure I'm OCD but I hope this helps!
Murat KAYA
10,127 Pointsit doesn't matter viewcontroller will handle everything.
Simen Anthonsen
1,097 PointsSimen Anthonsen
1,097 PointsThanks!!