Swiftで遊ぼう! on Hatena

あしたさぬきblogでやってた初心者オヤジのiOSプログラミング奮闘記がHatenaに来ました

Swiftで遊ぼう! - 221 - Developing iOS 8 Apps with Swift - Applying MVC switch再び

Swiftで遊ぼう!の古い記事-> Life-LOG OtherSide

つづき続き、ゆっくり進んでいます。どうも頭が固くなり理解力が落ちているためコーディングされた再帰法のイメージが掴めない(T_T)

それでもゆっくり進みます。

昨日のコーディングの続き。

func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op]) {
 if !ops.isEmpty {
  var remainingOps = ops
  let op = remainingOps.removeLast()
 // coding continue... 
 }
 return (nil, ops)
}

スタックの最初のアイテムを「op」に取り出しました。このopはイーナムですよね。どのケースが割りふられているのか判断して処理を変える必要があります。

ここでswitchを使うんです。

func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op]) {
 if !ops.isEmpty {
  var remainingOps = ops
  let op = remainingOps.removeLast()
  switch op {
   case .Operand(let operand):
    return (operand, remainingOps)
   case .UnaryOperation // coding continue
   case .BinaryOperation // coding continue
  }
 }
 return (nil, ops)
}

ます、数字のオペランドだった場合はシンプルですよね。.Operandのアソシエーションバリューを定数operandに入れて戻り値で返してます。remainingOpsは残っているアレーを戻してますね。

今日はここまで。