What the advantages & disadvantages to each of these approaches pertaining to creating file that takes care of the view configuration to reduce a controller's file size.
Main Purposes Are:
- Memory
- Performance
- Testing
- Usability
This is the simplest, capable of working, example to demonstrate the question, but when many views are present using many methods do any of the above concerns alter when comparing the Extension & ViewModel Class?
Reminder: The ViewModel Class or Extension would be placed in a separate file.
ViewModel Approach:
class VC: UIViewController {
lazy var viewModel: ViewModel {
return (main: self)
}()
override viewDidLoad() {
super.viewDidLoad()
initializeUI()
}
func initializeUI() {
viewModel.configureView()
}
}
class ViewModel {
private let main: UIViewController
init(main: UIViewController) {
self.main = main
}
func configureView() {
main.view.backgroundColor = UIColor.blue
}
}
Extension Approach:
class VC: UIViewController {
lazy var viewModel: ViewModel(main: self)
override viewDidLoad() {
super.viewDidLoad()
initializeUI()
}
func initializeUI() {
configureView()
}
}
extension VC {
func configureView() {
main.view.backgroundColor = UIColor.blue
}
}
-
\$\begingroup\$ Please see What to do when someone answers . If someone mentions an issue with your code in an answer, you can't just roll the advice back into the question. \$\endgroup\$200_success– 200_success2017年07月28日 06:45:40 +00:00Commented Jul 28, 2017 at 6:45
1 Answer 1
The viewModel property on the Extension example should be removed as it is redundant:
lazy var viewModel: ViewModel(main: self)
Irrespective of this, it seems unnecessary to create a separate ViewModel class solely for the purpose of encapsulating a configureView function. On that basis, the Extension approach seems like a much more sensible and performant way of managing this kind of configuration.
-
2\$\begingroup\$ No, you can't just edit the question like it's a collaborative coding wiki! Please put your suggestions in your own answer. \$\endgroup\$200_success– 200_success2017年07月27日 05:05:52 +00:00Commented Jul 27, 2017 at 5:05
-
\$\begingroup\$ Done. The edit was originally made the question because the original form actually slightly obscured the true difference between the two approaches, but happy to understand the convention here. More familiar with SO. \$\endgroup\$Duncan Babbage– Duncan Babbage2017年07月27日 05:13:01 +00:00Commented Jul 27, 2017 at 5:13
-
\$\begingroup\$ @Duncan Babbage Ah yes the lazy var was accidentally placed in the extension approach! And now looking back you are 100% correct, I appreciate it. \$\endgroup\$lifewithelliott– lifewithelliott2017年07月27日 06:37:27 +00:00Commented Jul 27, 2017 at 6:37
-
\$\begingroup\$ @DuncanBabbage the biggest thing with placing code in other files is the removal of private or fileprivate - It is definitely a struggle for me to figure out how to keep file size small while retaining private and fileprivate \$\endgroup\$lifewithelliott– lifewithelliott2017年07月28日 03:42:11 +00:00Commented Jul 28, 2017 at 3:42
Explore related questions
See similar questions with these tags.