? ? ? ? 這節(jié)課主要講了MVC架構(gòu),并且根據(jù)MVC架構(gòu)完成了第一個(gè)項(xiàng)目,計(jì)算器的開發(fā),下面簡單的記一下學(xué)習(xí)筆記,以及該項(xiàng)目的代碼。
? ? ? ? MVC中M指model,V指view,C指Controler,按照我的理解,M中主要就是函數(shù)庫,里面定義了各種函數(shù),完成具體的實(shí)現(xiàn)功能;V也就是前端的界面了,也就是swift中所見即所得的部分。C為控制器,有點(diǎn)像以前在c語言中用到的main函數(shù)一樣。MVC之間的關(guān)系呢,界面和模型之間不會(huì)有直接的交互,它們的交流都是通過控制器為媒介,串聯(lián)起來,其關(guān)系圖如下:

下面主要就是MVC框架的應(yīng)用了,計(jì)算器實(shí)現(xiàn)代碼如下:
[ViewController.swift]
//
//? ViewController.swift
//? Calculator
//
//? Created by Sergey Linnik on 10/12/16.
//? Copyright ? 2016 Sergey Linnik. All rights reserved.
//
import UIKit
classViewController:UIViewController{
? ? @IBOutlet private weak var display: UILabel!
? ? @IBOutlet private weak var history: UILabel!
? ? private var calculatorService: CalculatorService = CalculatorService()
? ? privatevaruserIsInProgress =false
? ? privatevardisplayText:Double{
? ? ? ? get{
? ? ? ? ? ? returnDouble(display.text!)!
? ? ? ? }
? ? ? ? set{
? ? ? ? ? ? display.text=String(newValue)
? ? ? ? }
? ? }
? ? @IBActionprivatefunctouchDigit(_sender:UIButton) {
? ? ? ? letdigit = sender.currentTitle!
? ? ? ? letcurrentTitle =display.text!
? ? ? ? if !userIsInProgress {
? ? ? ? ? ? display.text= digit
? ? ? ? }else{
? ? ? ? ? ? display.text= currentTitle + digit
? ? ? ? }
? ? ? ? userIsInProgress = true
? ? }
? ? @IBActionprivatefuncperformOperation(_sender:UIButton) {
? ? ? ? if userIsInProgress {
? ? ? ? ? ? calculatorService.setOperand(operand: displayText)
? ? ? ? ? ? userIsInProgress = false
? ? ? ? }
? ? ? ? ifletoperationType = sender.currentTitle{
? ? ? ? ? ? calculatorService.performOperation(symbol: operationType)
? ? ? ? }
? ? ? ? displayText = calculatorService.result
? ? }
? ? @IBActionfuncmakeHistory(_sender:UIButton) {
? ? ? ? ifhistory.text==nil{
? ? ? ? ? ? history.text=""
? ? ? ? }
? ? ? ? history.text=history.text! + sender.currentTitle!
? ? }
}
[CalculatorService.swift]
//
//? CalculatorService.swift
//? Calculator
//
//? Created by Sergey Linnik on 10/12/16.
//? Copyright ? 2016 Sergey Linnik. All rights reserved.
//
importFoundation
classCalculatorService {
? ? privatevaraccumulator =0.0
? ? privateenumOperation {
? ? ? ? caseConstant(Double)
? ? ? ? caseUnaryOperation((Double) ->Double)
? ? ? ? caseBinaryOperation((Double,Double) ->Double)
? ? ? ? caseEquals
? ? ? ? caseReset
? ? }
? ? varresult:Double{
? ? ? ? get{
? ? ? ? ? ? returnaccumulator
? ? ? ? }
? ? }
? ? private var pending: PendingBinaryOperationInfo?
? ? privatestructPendingBinaryOperationInfo {
? ? ? ? varbinaryOperation: (Double,Double) ->Double
? ? ? ? varfirstOperand:Double
? ? }
? ? funcsetOperand(operand:Double){
? ? ? ? accumulator= operand
? ? }
? ? privatevaroperations:Dictionary = [
? ? ? ? "AC":Operation.Reset,
? ? ? ? "π":Operation.Constant(M_PI),
? ? ? ? "e":Operation.Constant(M_E),
? ? ? ? "√": Operation.UnaryOperation(sqrt),
? ? ? ? "cos": Operation.UnaryOperation(cos),
? ? ? ? "±":Operation.UnaryOperation({ -$0 }),
? ? ? ? "×":Operation.BinaryOperation({$0 * $1}),
? ? ? ? "÷":Operation.BinaryOperation({$0 / $1}),
? ? ? ? "+":Operation.BinaryOperation({$0 + $1}),
? ? ? ? "-":Operation.BinaryOperation({$0 - $1}),
? ? ? ? "=":Operation.Equals,
? ? ? ? ".":Operation.BinaryOperation({Double("\($0).\($1)")! })
? ? ]
? ? funcperformOperation(symbol:String){
? ? ? ? ifletoperation =operations[symbol]{
? ? ? ? ? ? switchoperation {
? ? ? ? ? ? case.Constant(letvalue):
? ? ? ? ? ? ? ? accumulator= value
? ? ? ? ? ? case.UnaryOperation(letfunction):
? ? ? ? ? ? ? ? accumulator= function(accumulator)
? ? ? ? ? ? case.BinaryOperation(letfunction):
? ? ? ? ? ? ? ? executePendingOperation()
? ? ? ? ? ? ? ? pending=PendingBinaryOperationInfo(binaryOperation: function, firstOperand:accumulator)
? ? ? ? ? ? case.Reset:
? ? ? ? ? ? ? ? pending=nil
? ? ? ? ? ? ? ? accumulator=0.0
? ? ? ? ? ? case.Equals:
? ? ? ? ? ? ? ? executePendingOperation()
? ? ? ? ? ? default:break
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? privatefuncexecutePendingOperation() {
? ? ? ? ifpending!=nil{
? ? ? ? ? ? accumulator = pending!.binaryOperation(pending!.firstOperand, accumulator)
? ? ? ? ? ? pending=nil
? ? ? ? }
? ? }
}