Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Af64bc38c0ffcfcabdf430759ee491ce?s=47 Elvis Shi
September 16, 2016

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

A light-talk in 歌舞伎座.tech#11

Af64bc38c0ffcfcabdf430759ee491ce?s=128

Elvis Shi

September 16, 2016
Tweet

Transcript

  1. Վ෣ب࠲.tech#11 Swiftϓϩάϥϛϯάษڧձ LT(3)

  2. Who am I? MAGES. Inc. Game Div. iOS Developer @lovee

  3. ϋοΧιϯ GCD

  4. ϋοΧιϯʹ࢖͏ Swift ϋοΧιϯ͸͍͍ͧɻ

  5. ϋοΧιϯ஌Βͳ͍ਓʁ

  6. ϋοΧιϯࢀՃͨ͜͠ͱ͋Δਓʁ

  7. ϋοΧιϯͰ৆औͬͨ͜ͱ͋Δਓʁ

  8. None
  9. ϋοΧιϯʹඞཁͳ΋ͷ े෼ʹ଎͍Ϛγϯ • the New MacBook Ͱ΋े෼ ৴པͰ͖Δ஥ؒ •஥ؒɺେࣄɻͱͯ΋େࣄ ޮ཰ߴ͍Ξ΢τϓοτ

    •24࣌ؒલޙ͔͕࣌ؒ͠ͳ͍ ંΕͳ͍৺ˡ͜Εॏཁ •ϋοΧιϯʹϋϓχϯά͸͖ͭ΋ͷ
  10. ޮ཰ߴ͍Ξ΢τϓοτ ։ൃ։࢝લͷίʔσΟϯά͸ېࢭ OSS ͷ Library ΍ Framework ͸ར༻Մ ͦ͏ͩɺࣗ෼ͷ Framework

    Λ࡞Ζ͏ɻ
  11. Swift ͷಛ௃ Extension ରԠʹΑΔߴ͍֦ுੑ ීஈࣗ෼͕Α͘࢖͏ϝιουΛ Framework ʹ·ͱΊͪΌ͓͏

  12. 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)
  13. 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) } }
  14. var array = [Int](0 ..< 5) //[0, 1, 2, 3,

    4] ͋Εʁ γϟοϑϧͬͯ Ͳ͏΍ΔΜ͚ͩͬʁ
  15. 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]
  16. 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 } }
  17. https://github.com/el-hoshino/Eltaso

  18. Thank you

  19. Swift 3.0 ͷ GCD ࠓ·Ͱͷ GCD ͷྺ࢙ͷதͰ࠷େͷϦϦʔε

  20. GCDʢdispatch_…ʣΛ஌Βͳ͍ਓʁ

  21. ࠓ·Ͱ… ಉظॲཧʗඇಉظॲཧ 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#>)
  22. ͜Ε͔Β DispatchObject Ͱॲཧ͢Δ͜ͱʹ DispatchQueue DispatchSemaphore DispatchTime DispatchWorkItem …

  23. let imageView = UIImageView() let image = UIImage() //... dispatch_async(dispatch_get_main_queue())

    { imageView.image = image }
  24. let imageView = UIImageView() let image = UIImage() //... DispatchQueue.main.async

    { imageView.image = image }
  25. 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") }
  26. 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") }
  27. 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") }
  28. ৄ͘͠͸ https://developer.apple.com/ reference/dispatch ·Ͱ

  29. dispatch_once ͸͓๢͘ͳΓʹ… ୅ΘΓʹ static lazy var ͳͲΛ׆༻͠Ζͱͷ͜ͱ

  30. Thank you