Stanford University Open Class: IOS8
netease Space Start address link: Stanford University Open Class: IOS8 development
GitHub on subtitle link : subtitlesBaidu Net Disk resources download link : Baidu Network Disk
1. Calculator complete① enter key and related code
Defines an array that is used as a stack to hold data using var openStack = array<double> (); Enter to perform a stack operation @IBAction func enter () { //Set restart input string Userisinthemiddleoftypinganumber = False // Openstack.append value into the stack (displayvalue) println ("OpenStack = \ (OpenStack)") }
The displayvalue in this definition defines its get and set methods:
The data used to stack the var displayvalue:double{ get{//Converts the string to a Double return Nsnumberformatter (). Numberfromstring (display.text!)!. Doublevalue } set{ //Convert value to string Display.text = "\ (newvalue)" //Set restart input string Userisinthemiddleoftypinganumber = False } }
among the more commonly used two methods in the Nsnumberformatter class are:
func stringfromnumber (number:nsnumber),String ? NSNumber Convert to String
func numberfromstring (string:string),nsnumber? String transfer to Seoul NSNumber
Nsnumberformatter (). numberfromstring (display. text !)!. Doublevalue Converts the string to NSNumber, and then converts the NSNumber to a double.
Nsnumberformatter () is used to initialize the instance, and here is an implicit representation of the method in which the object is referenced.
② Subtraction Open Radical operation
Used to perform the Operation @IBAction func operate (Sender:uibutton) {//Gets the current button's title let operation = Sender.currenttitle If it is the middle input number if Userisinthemiddleoftypinganumber {//Enter the stack Enter ()}//SWITC H determine what operator is and then perform the corresponding operation switch operation! {case ' x ':p erformoperation (multiply) case ' ÷ ':p erformoperation ({(op1:double, op2:double)->double in Retu RN Op1/op2;}) Case ' + ':p erformoperation ({(OP1, OP2) in Op1 + OP2;}) Case "?":p erformoperation{$0-$ $ case "√":p erformoperation{sqrt ($)} Default:break}}// Defines a method for subtraction operations, where the parameter type is a method: (double, double)->double private func performoperation (Operation: (Double, double)- >double) {//stack must have two elements in order to perform subtraction operation if Openstack.count >= 2 {//to stack the last two elements and then perform the operation Displayvalue = operation (Openstack.removelast (), Openstack.removelast ()) Enter ()}}//define a method To open the square, the parameter type is a method: Double->double private func performoperation (operation:double->double) {//stack must have more than one element in order to open square if Openstac K.count >= 1 {displayvalue = operation (Openstack.removelast ()) Enter ()}}
a method can be used as a parameter of another method in ①swift:
performoperation (multiply) is to use the multiply method as a parameter of the Performoperation method
A method can be implicitly used as a parameter of another method in ②swift
performoperation ({(OP1:double, OP2:double),double inreturn OP1 + op2;})
You can see the implicit method as an argument, put it in {}, called the closure
in the back represents the action inside the method
③swift has the function of type Auto derivation, so it can be abbreviated as:
performoperation ( {(OP1, OP2) in op1 + op2;} )
The compiler in ④swift automatically passes a default value to the parameters in the method, the first argument is $ A, the second argument is $ ...
so you can continue to simplify to: performoperation ({$ + $})
⑤swift If one method is the last parameter of another method, you can put this method behind the original method:
performoperation (){$ + $}
There is no parameter in the method which can be removed directly (): performoperation () {$+$}
Here are some important ways to explain the overloaded problem:
The Swift language supports method overloading, and OC does not support overloading of methods:
func Performoperation (Operation: (double,double),double )
func performoperation (Operation:double)
the two methods, the same name, but not the same number of arguments, belong to the method overload. If you follow the course, there will be a problem with the direct use.
Error hint:Method ' perform ' with Objective-c selector ' perform: ' conflicts with previous declaration with the same objectiv E-c selector
The reason for this is that this class inherits from Uiviewcontroller, also inherits the NSObject, the compiler will then follow the attributes in OC to compile, so it will error.
Modifying this error method is simple:
1. Take the names of the two functions differently
2. Use an OC-unsupported type to block the swift language compiler from exposing this error in OC mode.
Research on 2.MVC design pattern
Explain:
1. The green arrow indicates the controller's direct reference to model and view. The direct reference is simple, and only the instance variables of model and view are defined in the controller . If the view is defined by storyboard or xib, there is iboutlet.
The 2.View communicates to the controller in three ways (three yellow arrows):
①action corresponds to target. For example, the Touch down,action (click button) in UIButton to add a method.
② set the view's delegate (proxy), such as Uitabviewdelegate, to implement the proxy in the controller.
③ sets the data source for the view, such as Uitableviewdatasource.
This allows the view to be able to communicate with the controller, without having to know who the controller is, and decoupling it from the controller.
3.the model graphic has a signal tower with notification & KVO next to it. Model is mainly through the notification and key-value monitoring to communicate with the controller.
4.Controller can communicate with view and model in MVC, but the model and view cannot communicate, separated by a yellow line.
=============================================================================================================== ================
demo:http://download.csdn.net/detail/misakahina/8824853
Swift Tour: 2. Complete the calculator to explore the MVC design pattern