斯坦福公开课之IOS开发:(2)

        这节课主要讲了MVC架构,并且根据MVC架构完成了第一个项目,计算器的开发,下面简单的记一下学习笔记,以及该项目的代码。

        MVC中M指model,V指view,C指Controler,按照我的理解,M中主要就是函数库,里面定义了各种函数,完成具体的实现功能;V也就是前端的界面了,也就是swift中所见即所得的部分。C为控制器,有点像以前在c语言中用到的main函数一样。MVC之间的关系呢,界面和模型之间不会有直接的交互,它们的交流都是通过控制器为媒介,串联起来,其关系图如下:

MVC关系图

下面主要就是MVC框架的应用了,计算器实现代码如下:


[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

        }

    }

}

你可能感兴趣的:(斯坦福公开课之IOS开发:(2))