2014年9月1日月曜日

開発環境

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

SHARPEN YOUR PENCIL(p.340)

コード(Xcode)

main.swift

//
//  main.swift
//  sample340
//
//  Created by kamimura on 9/1/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation


let ships:[[String:[String]]] = [
    ["locations":["31", "41", "51"], "hits":["", "", ""]],
    ["locations":["14", "24", "34"], "hits":["", "hit", ""]],
    ["locations":["00", "01", "02"], "hits":["hit", "", ""]]]

let ship1:[String:[String]] = ships[0]
let ship2:[String:[String]] = ships[1]
let ship3:[String:[String]] = ships[2]

var locations:[String]
var hits:[String]

locations = ship2["locations"]!
println("Location is \(locations[1])") // 24

hits = ship3["hits"]!
if hits[0] == "hit" {
    println("Ouch, hit on third ship at location one")
}

hits = ship1["hits"]!
hits[2] = "hits"
println(hits)

入出力結果(Console Output)

Location is 24
Ouch, hit on third ship at location one
[, , hits]
Program ended with exit code: 0

0 コメント:

コメントを投稿