티스토리 뷰

Lesson 1.

 우린 앞으로 계산기를 만들어 볼거에요

스위프트를 쓸거고요 그래서 IDE인 xCode를 좀 봐야겠어요 대충 설명해드릴게요.

제일 중요한 개념인 Optional 을 설명해준다.

옵셔널은 not set or something

? means Optional Type

! means unwrap Optional Value 

If I put ! mark after nil value -> Program will crushed!!!


display.text = display.text! + digit

all variable in Swift must be "INITIALIZED"


Lesson 2.

Calculator continued...

swift is strongly typed language, so you don't have to mention " : Blahblah "

var displayValue: Double {

  get {

    return NSNumberFormatter().numberFromString(display.text!)!.doubleValue

  }

  set {

    display.text = "\(newValue)"

    userIsInTheMiddleOfTypingANumber = false

  }

}


var operandStack = Array<Double>()

operandStack.append(displayValue)

그냥 이렇게 쓰면 되는듯 겟 셋을 써두고 맘대로 쓴담에 newValue로 들어오는걸 쉽게 가져올수 있나 봄.


if operandStack.count >=2 {

 operandStack.removeLast() 

}


func performOperation(operation: (Double, Double) -> Double) {

  displayValue = operation( a.removeLast(), a.removeLast() )

  enter()

}


performOperation(         {     ( op1: Double, op2: Double) -> Double           in        return op1 * op2     }      )

 여기서 :Double 부분은 타입추축이 가능하기때문에 삭제해도 무방

 { (op1, op2)  in op1 * op2 }

{ (op1, op2) in $0 * $1 )

{ $0 * $1 }

it covers!!!!!!


Auto layout

yellow means it placed in wrong place

you can see 4 buttons on right down  and it gives you some bulk autolayout option


it covers all kinds of devices


Lesson 3 MVC Model

Creating Model - CalculatorBrain.swift

import Foundation

class Calculation {

enum Op {

case Operand(Double)

case UnaryOperation(Stri ng, Double -> Double)

case BinaryOperation(String, (Double, Double) -> Double )

}


var opStack = [Op]()

var konwnOps = Dictionary<String, Op>() //Dictionary <Key, Value>

-> can be

var knownOps = [String:Op]()


init(){

knownOps["*"] = Op.BinaryOperation("*") { $0 * $1 }

knownOps["*"] = Op.BinaryOperation("*") { $0 * $1 } // 나누기  빼기 더하기 어쩌구.. 귀찮으니 생략

// BlahBlah........

knownOps["루트"] = Op.UnaryOperation("root" ,  sqrt )

}

func evaluate( ops: [Op]) -> (result: Double?, remainOps: [Op])

{

if !ops.isEmpty{

var remainingOps = ops

let op = remainingOps.removeLast()

switch op{

case .Operand(let operand):

return (operand,  reaminingOps)

case .UnaryOperation(_, let operation):

let operandEvaluation = evlaute(remainingOps)

if let operand = operandEvaluation.result {

return (operation(operand), operandEvaluation.remainingOps)

}

case .BinaryOpration(_, let operation):

let op1Evaluation = evlauate(remainingOps)

if let operand1 = op1Evaluation.result {


}

}

}


func pushOperand(operand: Double) {

opStack.append(Op.Operand(operand))

}

func performOperation(symbol: String){

if let operation = knownOps[symbol] {

opStack.append(operation)

}

}


}

그 enum : printable

var description {

get: blahblah 로 프린트 가능.

}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함