開発環境
- OS X Mavericks - Apple(OS)
- Xcode 6.0 Beta
- Swift (プログラミング言語)
Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 10(Liberated functions: First Class Functions)、BE THE BROWSER(p.438)をSwiftで考えてみる。
BE THE BROWSER(p.438)
コード(Xcode)
main.swift
// // main.swift // sample438 // // Created by kamimura on 9/7/14. // Copyright (c) 2014 kamimura. All rights reserved. // import Foundation let midi:Bool = true var midiInterface:() -> () var midiInterfaceOptional:(() -> ())? var midiInterfaceOptional1:(() -> ())? // play() Use of unresolved identifier 'play' func play() { println("play") } play() // myPause() Use of unresolved identifier 'myPause' let myPause = {() in println("pause")} myPause() // stop() Use of unresolved identifier 'stop' func stop() { println("stop") } stop() // createMidi() Use of unresolved identifier 'createMidi' func createMidi() { println("createMidi") } createMidi() // midiInterface() Variable 'midiInterface' used before being initialized if midi { midiInterface = {() in println("midiInterface")} midiInterface() } // midiInterface() Variable 'midiInterface' used before being initialized println("Optional") println("before") midiInterfaceOptional?() if midi { println("if") midiInterfaceOptional = {() in println("midiInterfaceOptional")} midiInterfaceOptional?() } println("after") midiInterfaceOptional?() println("before1") if !midi { println("if1") midiInterfaceOptional1 = {() in println("midiInterfaceOptional1")} midiInterfaceOptional1?() } println("after1") midiInterfaceOptional1?()
入出力結果(Console Output)
play pause stop createMidi midiInterface Optional before if midiInterfaceOptional after midiInterfaceOptional before1 after1 Program ended with exit code: 0
0 コメント:
コメントを投稿