LoginSignup
3
3

More than 5 years have passed since last update.

第3回名古屋Swiftハッカソン(Swift初心者向けワークショップもあり)に参加した

Last updated at Posted at 2014-12-06

「Swift初心者向け」で参加して、A Swift Tourをやってました。

Playgroundで色々試していたので公開。コピペで動作するはず。

Protocol

// 定義
protocol ExampleProtocol {
    var simpleDescription: String { get }
    func adjust()
}

// 使い方
class SimpleClass: ExampleProtocol {
    var simpleDescription: String = "A very simple class."
    var anotherProperty: Int = 69105
    func adjust() {
        simpleDescription += "  Now 100% adjusted."
    }
}
var a = SimpleClass()
a.adjust()
let aDescription = a.simpleDescription

enum

// 定義
enum ServerResponse {
    case Result(String, String)
    case Response(String)
    case Error(String)
}

let success = ServerResponse.Result("6:00 am", "8:09 pm")
let failure = ServerResponse.Error("Out of cheese.")
let response = ServerResponse.Response("404")

// 使い方

switch response {

case let ServerResponse.Result(sunrise, sunset):
    let serverResponse = "Sunrise is at \(sunrise) and sunset is at \(sunset)."

case let ServerResponse.Error(error):
    let serverResponse = "Failure...  \(error)"

case let ServerResponse.Response(response):
    let serverResonse = "Status: \(response)"
}

Y Combinator

// 定義
func fix(f: (Int -> Int, Int) -> Int) -> (Int -> Int) {
    var fix_f: (Int -> Int)!
    fix_f = {(n: Int) -> Int in return f(fix_f, n)}
    return fix_f
}

// 使い方
let fib = fix {(f: Int -> Int, n: Int) -> Int in // フィボナッチ
    return n < 2 ? n : f(n - 1) + f(n - 2)
}

Delegate

// 定義
protocol BroadcasterProtocol{
    func broadcast() -> String
}

class LocalBroadcaster:BroadcasterProtocol {
    func broadcast() -> String{
        return "On Air!"
    }
}

class Broadcaster {
    var delegate: BroadcasterProtocol?

    func broadcast() -> String? {
        return self.delegate?.broadcast()
    }
}


// 使い方

let broadcaster = Broadcaster()
let localBroadcaster = LocalBroadcaster()
let listen = broadcaster.broadcast()

broadcaster.delegate = localBroadcaster
let listen2 = broadcaster.broadcast()

Singleton

// 定義
class Singleton {
    class var sharedInstance : Singleton {
        struct Static {
            static let instance : Singleton = Singleton()
        }
        return Static.instance
    }
}

willSet

class History {
    var history:[Int]

    init(){
        self.value = 0
        self.history = []
    }

    var value:Int{
        willSet{
            self.history.append(self.value)
        }
    }

}

// test
let history = History()

history.value = 1
history.value = 2
history.value = 3

history.value
history.history

class PoppableHistory: History {
    func pop() {
        if self.history.count > 0 {
            self.value = self.history.last!
            self.history.removeLast()
            self.history.removeLast()
        }
    }
}

// test
let poppableHistory = PoppableHistory()

poppableHistory.value = 1
poppableHistory.value = 2
poppableHistory.value = 3

poppableHistory.value
poppableHistory.history

poppableHistory.pop()

poppableHistory.value
poppableHistory.history

poppableHistory.pop()
poppableHistory.pop()
poppableHistory.pop()

poppableHistory.value
poppableHistory.history

for文

func repeat<Item>(item: Item, times: Int) -> [Item] {
    var result = [Item]()
    for i in 0..<times {
        result.append(item)
    }
    return result
}

repeat(2, 4)

最後に

講師を努めてくださった村田 知常さん、ありがとうございました。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3