Constants.swift question
Closed this issue · 1 comments
Hi Peter. Caveat - I'm a complete coding and GitHub newbie, so apologies if this is not the right way to post a question or comment, but here we go...
First of all - THANK YOU for posting this code. I was really struggling to comprehend some of the initial concepts (I'm following the course via iTunesU) and this really has helped me and inspired me to keep plugging away. I'm a 50 year old US expat currently living in Hong Kong, and just starting to attempt to learn Swift coding in my spare time. I'm a marketing/ops/finance guy by day...
I was trying to understand why you created a separate Constants.swift file for that one Constants struct? Since Foundation is imported into CalculatorBrain.swift and Constants.swift, why wouldn't you have simply written that struct in CalculatorBrain.swift? Does that somehow violate the principles of MVC?
Thanks in advance.
-Jim
Hi Jim,
By default GitHub has a label question for issues, so it is a proper way to ask questions.
The Constants.numberOfDigitsAfterDecimalPoint
is used on two places: once in CalculatorBrain.swift and once in ViewController.swift. As the constant is used in two different files, I decided to put the constant in a separate file.
Perhaps even a better solution is to make the complete numberFormatter
code common to make sure numbers in the sequence display and the big display are formatted exactly the same.
I am triggered by your remark about violating the principles of MVC. In theory, the model should not be aware how numbers are represented. However the last sentence of extra credit 2 is saying:
You can do all this for your
description
in theCalculatorBrain
as well.
That's way I just duplicated the code.
A nicer solution is to pass a numberFormatter
from the controller to the model, but you have to add e.g. a function in CalculatorBrain
like func formatNumbers(using numberFormatter: NumberFormatter)
.
Peter