2014年9月8日月曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 10(Liberated functions: First Class Functions)、EXERCISE(p.445)をSwiftで考えてみる。

EXERCISE(p.445)

コード(Xcode)

main.swift

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

import Foundation

struct Passenger {
    let name:String
    let paid:Bool
}

let passengers:[Passenger] = [
    Passenger(name: "Jane Doloop", paid: true),
    Passenger(name: "Dr. Evel", paid: true ),
    Passenger(name: "Sue Property", paid: false),
    Passenger(name: "John Funcall", paid: true)
]

func checkNoFlyList(passenger:Passenger) -> Bool {
    return passenger.name == "Dr. Evel"
}

func checkNotPaid(passenger:Passenger) -> Bool {
    return !passenger.paid
}

for passenger in passengers {
    println("\(passenger.name) \(checkNoFlyList(passenger)) \(checkNotPaid(passenger))")
}

入出力結果(Console Output)

Jane Doloop false false
Dr. Evel true false
Sue Property false true
John Funcall false false
Program ended with exit code: 0

0 コメント:

コメントを投稿