ハッカソンに使うswift & swift 3.0のgcd

30
歌舞伎座 .tech#11 Swift プログラミング勉強会 LT(3 )

Upload: -

Post on 16-Apr-2017

82 views

Category:

Software


5 download

TRANSCRIPT

Page 1: ハッカソンに使うSwift & Swift 3.0のGCD

歌舞伎座.tech#11 Swiftプログラミング勉強会

LT(3)

Page 2: ハッカソンに使うSwift & Swift 3.0のGCD

Who am I?

MAGES. Inc. Game Div.

iOS Developer

@lovee

Page 3: ハッカソンに使うSwift & Swift 3.0のGCD

ハッカソン GCD

Page 4: ハッカソンに使うSwift & Swift 3.0のGCD

ハッカソンに使う Swiftハッカソンはいいぞ。

Page 5: ハッカソンに使うSwift & Swift 3.0のGCD

ハッカソン知らない人?

Page 6: ハッカソンに使うSwift & Swift 3.0のGCD

ハッカソン参加したことある人?

Page 7: ハッカソンに使うSwift & Swift 3.0のGCD

ハッカソンで賞取ったことある人?

Page 8: ハッカソンに使うSwift & Swift 3.0のGCD
Page 9: ハッカソンに使うSwift & Swift 3.0のGCD

ハッカソンに必要なもの十分に速いマシン • the New MacBook でも十分

信頼できる仲間 •仲間、大事。とても大事

効率高いアウトプット •24時間前後しか時間がない

折れない心←これ重要 •ハッカソンにハプニングはつきもの

Page 10: ハッカソンに使うSwift & Swift 3.0のGCD

効率高いアウトプット

開発開始前のコーディングは禁止

OSS の Library や Framework は利用可

そうだ、自分の Framework を作ろう。

Page 11: ハッカソンに使うSwift & Swift 3.0のGCD

Swift の特徴

Extension 対応による高い拡張性

普段自分がよく使うメソッドを Framework にまとめちゃおう

Page 12: ハッカソンに使うSwift & Swift 3.0のGCD

var id = UIDevice.current.identifierForVendor?.description ?? “"

let remainingIndex = id.characters.index(id.startIndex, offsetBy: 8, limitedBy: id.endIndex) ?? id.endIndex

id = id.substring(to: remainingIndex)

Page 13: ハッカソンに使うSwift & Swift 3.0のGCD

var id = UIDevice.current.identifierForVendor?.description ?? “"

id.keepFirst(8)

// Extension extension String { public func keepingFirst(_ n: Int = 1) -> String { let remainingIndex = self.characters.index(self.startIndex, offsetBy: n, limitedBy: self.endIndex) ?? self.endIndex return self.substring(to: remainingIndex) } }

extension String { public mutating func keepFirst(_ n: Int = 1) { self = self.keepingFirst(n) } }

Page 14: ハッカソンに使うSwift & Swift 3.0のGCD

var array = [Int](0 ..< 5) //[0, 1, 2, 3, 4]

あれ? シャッフルって

どうやるんだっけ?

Page 15: ハッカソンに使うSwift & Swift 3.0のGCD

var array = [Int](0 ..< 5) //[0, 1, 2, 3, 4]

// Fisher-Yates Shuffle Algorithm for i in array.indices.reversed().dropLast() { let j = Int(arc4random_uniform(UInt32(i))) (array[i], array[j]) = (array[j], array[i]) } //[1, 4, 0, 2, 3]

Page 16: ハッカソンに使うSwift & Swift 3.0のGCD

extension Array { public var shuffled: Array<Element> { var array = self for i in array.indices.reversed().dropLast() { let j = Int(arc4random_uniform(UInt32(i))) (array[i], array[j]) = (array[j], array[i]) } return array } public mutating func shuffle() { self = self.shuffled } }

var array = [Int](0 ..< 5) //[0, 1, 2, 3, 4] array.shuffle() //[1, 4, 0, 2, 3]

Page 17: ハッカソンに使うSwift & Swift 3.0のGCD

https://github.com/el-hoshino/Eltaso

Page 18: ハッカソンに使うSwift & Swift 3.0のGCD

Thank you

Page 19: ハッカソンに使うSwift & Swift 3.0のGCD

Swift 3.0 の GCD今までの GCD の歴史の中で最大のリリース

Page 20: ハッカソンに使うSwift & Swift 3.0のGCD

GCD(dispatch_…)を知らない人?

Page 21: ハッカソンに使うSwift & Swift 3.0のGCD

今まで…

同期処理/非同期処理 dispatch_sync(<#dispatch_queue_t#>, <#() -> Void#>) dispatch_async(<#dispatch_queue_t#>, <#() -> Void#>)

メインキュー/グローバルキュー dispatch_get_main_queue() dispatch_get_global_queue(<#Int#>, <#UInt#>)

セマフォ dispatch_semaphore_create(<#Int#>) dispatch_semaphore_signal(<#dispatch_semaphore_t#>) dispatch_semaphore_wait(<#dispatch_semaphore_t#>, <#dispatch_time_t#>)

Page 22: ハッカソンに使うSwift & Swift 3.0のGCD

これから

DispatchObject で処理することに DispatchQueue DispatchSemaphore DispatchTime DispatchWorkItem …

Page 23: ハッカソンに使うSwift & Swift 3.0のGCD

let imageView = UIImageView() let image = UIImage()

//...

dispatch_async(dispatch_get_main_queue()) { imageView.image = image }

Page 24: ハッカソンに使うSwift & Swift 3.0のGCD

let imageView = UIImageView() let image = UIImage()

//...

DispatchQueue.main.async { imageView.image = image }

Page 25: ハッカソンに使うSwift & Swift 3.0のGCD

let semaphore = dispatch_semaphore_create(0)

dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)) { //Data Downloading... dispatch_semaphore_signal(semaphore) }

let waitTime = dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC) * 60) if dispatch_semaphore_wait(semaphore, waitTime) == 0 { print(“Succeeded") } else { print("Failed") }

Page 26: ハッカソンに使うSwift & Swift 3.0のGCD

let semaphore = DispatchSemaphore(value: 0)

DispatchQueue.global().async { //... semaphore.signal() }

let waitTime = DispatchTime.now() + DispatchTimeInterval.seconds(60) if semaphore.wait(timeout: waitTime) == .success { print("Succeeded") } else { print("Failed") }

Page 27: ハッカソンに使うSwift & Swift 3.0のGCD

let semaphore = DispatchSemaphore(value: 0)

DispatchQueue.global().async { //... semaphore.signal() }

let waitTime = DispatchTime.now() + .seconds(60) if semaphore.wait(timeout: waitTime) == .success { print("Succeeded") } else { print("Failed") }

Page 28: ハッカソンに使うSwift & Swift 3.0のGCD

詳しくは https://developer.apple.com/reference/dispatch まで

Page 29: ハッカソンに使うSwift & Swift 3.0のGCD

dispatch_once はお亡くなりに…代わりに static lazy var などを活用しろとのこと

Page 30: ハッカソンに使うSwift & Swift 3.0のGCD

Thank you