Mobile App Development for iOS
Notes Topics Weekly Resources Graded work Professor Code examplesRecently, the professor briefly discussed a suggested code organization technique for use in a view controller class.
In a view controller, we suggest the following top-to-bottom organizational scheme:
When your controller code grows to hundreds of lines, this scheme will help.
Here’s how to implement this scheme: Add specially-formatted comments to separate each of the four groups/sections. Each comment begins with the string “MARK: - “. For example:
// MARK: - Instance variables
Here’s a full example of a controller that implements this scheme:
class ViewController: UIViewController {
// MARK: - Instance variables
var message = "Hello, world!"
var currentValue = 12
// MARK: - Outlets
@IBOutlet weak var gpaValue: UITextField!
@IBOutlet weak var gpaTitle: UILabel!
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// this function was included in the new project template
// add more code here
}
func doSomething(_ input: String) -> String {
// this function was added by the programmer
// add code here
return ""
}
// MARK: - Actions
@IBAction func programChanged(_ sender: UISegmentedControl) {
// add code here
}
}
When you do this, you will enjoy one more benefit while writing code. Xcode will nicely format the list of class members when you click (or Ctrl+6) the “document items” part of the jump bar.
As you can see, each group/section is separated by a horizontal line, and has a bold title.
Incidentally, this list is searchable. Just begin typing, and it will filter the list to include only those members that match the typed text.
Sweet.