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
How did I build an Open-Source SwiftUI Toast Library
lovee
1
48
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
660
SwiftUIで二重スクロール作ってみた / When I tried to make a dual-scroll-ish view in SwiftUI
lovee
1
240
Observation のあれこれ / A brief introduction about Observation
lovee
3
330
ChatGPT 時代の勉強 / Learning under ChatGPT era
lovee
26
8.2k
属人化しない為の勉強会作り / To make tech meetups with less personal dependencies
lovee
0
270
偏見と妄想で語るスクリプト言語としての Swift / Swift as a Scripting Language
lovee
2
790
danger-swift-kantoku
lovee
1
490
Decimal、正しく使ってる? / Are you using Decimal correctly?
lovee
4
690
Other Decks in Programming
See All in Programming
Outline View in SwiftUI
1024jp
1
330
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
Jakarta EE meets AI
ivargrimstad
0
160
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
Remix on Hono on Cloudflare Workers
yusukebe
1
290
CSC509 Lecture 12
javiergs
PRO
0
160
みんなでプロポーザルを書いてみた
yuriko1211
0
260
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Building Adaptive Systems
keathley
38
2.3k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Automating Front-end Workflow
addyosmani
1366
200k
A Modern Web Designer's Workflow
chriscoyier
693
190k
How to Ace a Technical Interview
jacobian
276
23k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
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