斯坦福公開課之IOS開發(fā):(2)

? ? ? ? 這節(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關(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

? ? ? ? }

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 2014年的蘋果全球開發(fā)者大會(huì)(WWDC),當(dāng)Craig Federighi向全世界宣布“We have new ...
    yeshenlong520閱讀 2,394評(píng)論 0 9
  • 六年前的晚上,雖然沒有雪,緣分卻通過電話將我們聯(lián)系到了一起。你那好聽的不知用什么來形容的聲音,讓我明白了不只是一見...
    心游萬仞2011閱讀 535評(píng)論 0 0
  • 前幾天常老師給孩子們布置了一項(xiàng)仿寫作業(yè),仿寫的內(nèi)容是史鐵生的《我與地壇》。主要是讓孩子們以文中:“如果以...
    蒹葭essay閱讀 2,101評(píng)論 0 3
  • 是時(shí)候把心拿出來曬曬,不然都霉了,臭了,黑了,該扔了。 畢業(yè)回國后工作了一陣子,說實(shí)話,有很多不適應(yīng)。路更寬了,但...
    李小胖的媽媽閱讀 1,013評(píng)論 -1 16

友情鏈接更多精彩內(nèi)容