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

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

Elvis Shi
September 16, 2016

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

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

Elvis Shi

September 16, 2016
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

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

    4] ͋Εʁ γϟοϑϧͬͯ Ͳ͏΍ΔΜ͚ͩͬʁ
  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]
  5. 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 } }
  6. ࠓ·Ͱ… ಉظॲཧʗඇಉظॲཧ 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#>)
  7. 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") }
  8. 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") }
  9. 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") }