This and the expression evaluation in the last article are the contents of the experiment of IOS course. The teacher demonstrated a version realized by NSExpression. Let’s implement = = by expression evaluation
Implemented using NSExpression
What is NSExpression
This is not introduced in the basic tutorial, and there are few Chinese introductions.There is an article on Nshipster, but I still don’t know much about NSExpression after reading it, so I will add this after studying the official documents later.
Train of thought
I’m going to use the MVC model.
-
So let’s just draw a calculator, a simple calculator that can do four operations.
-
Model mainly implements the following three methods:
-(void)delNumber; // backspace -(NSString *)compute; / / computing - (void) clearSpace; // Clear the input
-
Finally, all the ViewController needs to do is “assemble” the characters typed by the user into strings and hand them over to the Model to process.
Something to watch out for
-
In viewDidLoad, when the view controller initializes, make sure to generate an instance of the calculator(Model) :
self.calculator = [[Calculator alloc]init];
-
Overriding the getter method in Calculator ensures that we get an object each time, rather than a null pointer
-
(Calculator *)calculator
{ if (! _calculator) { _calculator = [[Calculator alloc]init]; } return _calculator; }
-
-
Special character multiplier and division buttons are entered in special characters and converted to * / symbols to perform calculations.
To realize the rounded
Adding a little Angle to a button can turn it into a rounded corner. The key is where to put the modified code.
-
Use ViewWillAppear when the view is displayed
-
Create a new class btnView using awakeFromNib, inherited from UIButton, so it can override awakeFromNib’s methods.
Every time the button is awakened, it will be called. Change the class of the button to BTNView.
Using numerical expressions
Train of thought
You can use the Model of the previous numerical expression directly, so there are only two things left to do: one is to “draw” the calculator’s interface, and the other is to “assemble” the input string.
Problems encountered
Adds an equal sign to the end of the string
Since my Model was designed to enter “=” at the end of the expression to calculate the output result, when the user presses “=”, add a “=” at the end of the input string before sending it into the ExpressionCalculate method of the Model. This “=” can be obtained directly from the titleLabel of a button.
NSMutableString *computeStr = [NSMutableString stringWithString:self.calculator.input];
[computeStr appendString:[[sender titleLabel] text]];
Continuous calculation
So the first thing that’s going to happen is I’m going to have a sequential failure, like 10*10+20, and I’m going to get 120, and then I’m going to press +10=x2 so I’m going to get (120+10)x2 and I’m going to get 140 instead of 260. The reason is that while the string in the TextField has changed, the input in the Calculator class has not changed. For example, the above formula would be the input = 10×10+20+10×2 string for continuous calculation. After each calculation, change the input values to be the same as the values in TextFiled.
Double backspace?
DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber = DelNumber So it’s like backspace twice. The input method should not call the delNumber method.
other
-
The ‘-‘ on the special character minus sign is inserted by the mathematical symbol inside the special character, so isEqualToString: is not the operation ‘-‘, delete it and enter it directly from the keyboard.
But if you use NSExpression, you don’t have to change it. (And this shows how powerful NSExpression really is.
To improve
Illegal input check √
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = The existing isOperator method can be used to determine, but note that parentheses and other operators can occur consecutively.
Integers also have a lot of decimal root
In order to calculate the double defined, but then every time the result is a lot of 0’s, forget the decimal, the integer result is also the same, look at the good… If 0 is an integer, then it is an integer. If 0 is an integer, then it is a floating point number = =.
rendering