Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ハッカソンに使うSwift & Swift 3.0のGCD
Search
Elvis Shi
September 16, 2016
Programming
0
480
ハッカソンに使うSwift & Swift 3.0のGCD
A light-talk in 歌舞伎座.tech#11
Elvis Shi
September 16, 2016
Tweet
Share
More Decks by Elvis Shi
See All by Elvis Shi
個人アプリを2年ぶりにアプデしたから褒めて / I just updated my personal app, praise me!
lovee
0
340
How did I build an Open-Source SwiftUI Toast Library
lovee
1
69
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
790
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
lovee
1
270
Observation のあれこれ / A brief introduction about Observation
lovee
3
350
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
27
8.6k
属人化しない為の勉強会作り / To make tech meetups with less personal dependencies
lovee
0
300
偏見と妄想で語るスクリプト言語としての Swift / Swift as a Scripting Language
lovee
2
830
danger-swift-kantoku
lovee
1
520
Other Decks in Programming
See All in Programming
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
4
370
チームリードになって変わったこと
isaka1022
0
190
技術を根付かせる / How to make technology take root
kubode
1
240
Multi Step Form, Decentralized Autonomous Organization
pumpkiinbell
1
660
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
110
SwiftUI Viewの責務分離
elmetal
PRO
0
150
Amazon Bedrock Multi Agentsを試してきた
tm2
1
280
sappoRo.R #12 初心者セッション
kosugitti
0
230
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
4
1.3k
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
0
180
Unity Android XR入門
sakutama_11
0
140
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2.2k
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
328
21k
Designing for Performance
lara
604
68k
Building Your Own Lightsaber
phodgson
104
6.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
It's Worth the Effort
3n
184
28k
Writing Fast Ruby
sferik
628
61k
Unsuck your backbone
ammeep
669
57k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
310
Building a Scalable Design System with Sketch
lauravandoore
460
33k
GitHub's CSS Performance
jonrohan
1030
460k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Transcript
Վب࠲.tech#11 Swiftϓϩάϥϛϯάษڧձ LT(3)
Who am I? MAGES. Inc. Game Div. iOS Developer @lovee
ϋοΧιϯ GCD
ϋοΧιϯʹ͏ Swift ϋοΧιϯ͍͍ͧɻ
ϋοΧιϯΒͳ͍ਓʁ
ϋοΧιϯࢀՃͨ͜͠ͱ͋Δਓʁ
ϋοΧιϯͰऔͬͨ͜ͱ͋Δਓʁ
None
ϋοΧιϯʹඞཁͳͷ ेʹ͍Ϛγϯ • the New MacBook Ͱे ৴པͰ͖Δؒ •ؒɺେࣄɻͱͯେࣄ ޮߴ͍Ξτϓοτ
•24࣌ؒલޙ͔͕࣌ؒ͠ͳ͍ ંΕͳ͍৺ˡ͜Εॏཁ •ϋοΧιϯʹϋϓχϯά͖ͭͷ
ޮߴ͍Ξτϓοτ ։ൃ։࢝લͷίʔσΟϯάېࢭ OSS ͷ Library Framework ར༻Մ ͦ͏ͩɺࣗͷ Framework
Λ࡞Ζ͏ɻ
Swift ͷಛ Extension ରԠʹΑΔߴ͍֦ுੑ ීஈ͕ࣗΑ͘͏ϝιουΛ Framework ʹ·ͱΊͪΌ͓͏
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)
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) } }
var array = [Int](0 ..< 5) //[0, 1, 2, 3,
4] ͋Εʁ γϟοϑϧͬͯ Ͳ͏ΔΜ͚ͩͬʁ
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]
var array = [Int](0 ..< 5) //[0, 1, 2, 3,
4] array.shuffle() //[1, 4, 0, 2, 3] // Extension 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 } }
https://github.com/el-hoshino/Eltaso
Thank you
Swift 3.0 ͷ GCD ࠓ·Ͱͷ GCD ͷྺ࢙ͷதͰ࠷େͷϦϦʔε
GCDʢdispatch_…ʣΛΒͳ͍ਓʁ
ࠓ·Ͱ… ಉظॲཧʗඇಉظॲཧ 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#>)
͜Ε͔Β DispatchObject Ͱॲཧ͢Δ͜ͱʹ DispatchQueue DispatchSemaphore DispatchTime DispatchWorkItem …
let imageView = UIImageView() let image = UIImage() //... dispatch_async(dispatch_get_main_queue())
{ imageView.image = image }
let imageView = UIImageView() let image = UIImage() //... DispatchQueue.main.async
{ imageView.image = image }
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") }
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") }
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") }
ৄ͘͠ https://developer.apple.com/ reference/dispatch ·Ͱ
dispatch_once ͓͘ͳΓʹ… ΘΓʹ static lazy var ͳͲΛ׆༻͠Ζͱͷ͜ͱ
Thank you