2014年8月29日金曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 8(Bringing it All Together: Building an app)、EXERCISE(p.330)をSwiftで考えてみる。

EXERCISE(p.330)

コード(Xcode)

main.swift

//
//  main.swift
//  sample330
//
//  Created by kamimura on 8/29/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

let view:Dictionary<String, (AnyObject, AnyObject?) -> ()> = [
    "displayMessage":{(msg, a) -> () in println(msg)},
    "displayHit":{(row, col) -> () in println("Hit!(\(row), \(col!))")},
    "displayMiss":{(row, col) -> () in println("Miss!(\(row), \(col!))")}]

view["displayMessage"]?("Dictionary", nil)
view["displayHit"]?(1, 2)
view["displayMiss"]?(3, 4)

struct View1 {
    let displayMessage:(String) -> ()
    let displayHit:(Int, Int) -> ()
    let displayMiss:(Int, Int) -> ()
}

let view1:View1 = View1(
    displayMessage: {(msg) -> () in println(msg)},
    displayHit: {(row, col) -> () in println("Hit!(\(row), \(col))")},
    displayMiss: {(row, col) -> () in println("Miss!(\(row), \(col))")})

view1.displayMessage("struct")
view1.displayHit(1, 2)
view1.displayMiss(3, 4)

class View2 {
    func displayMessage(msg:String) {
        println(msg)
    }
    func displayHit(row:Int, col:Int) {
        println("Hit!(\(row), \(col))")
    }
    func displayMiss(row:Int, col:Int) {
        println("Miss!(\(row), \(col))")
    }
}

let view2:View2 = View2()
view2.displayMessage("class")
view2.displayHit(1, col: 2)
view2.displayMiss(3, col: 4)

入出力結果(Console Output)

Dictionary
Hit!(1, 2)
Miss!(3, 4)
struct
Hit!(1, 2)
Miss!(3, 4)
class
Hit!(1, 2)
Miss!(3, 4)
Program ended with exit code: 0

0 コメント:

コメントを投稿