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

The newest super hissatsu-waza of iOS app devel...

cockscomb
April 15, 2015

The newest super hissatsu-waza of iOS app development

Presented at potatotips #16

謝辞

必殺技の名前を考えるのに貢献してくれたyashiganiさんにこの場を借りてお礼申し上げます。

cockscomb

April 15, 2015
Tweet

More Decks by cockscomb

Other Decks in Programming

Transcript

  1. let intLike = ["12.3", "45", "6-7-8"] let results = intLike.map

    { $0.toInt() } println(results) // => [nil, Optional(45), nil]
  2. let intLike = ["12.3", "45", "6-7-8"] let results = intLike.map

    { $0.toInt() }.filter { $0 != nil }.map { $0! } println(results) // => [45]
  3. let intLike = ["12.3", "45", "6-7-8"] let results = intLike.flatMap

    { $0.toInt().map { [ $0 ] } ?? [] } println(results) // => [45] New in Swift 1.2
  4. class DragonOperation: NSOperation { let string: String var result: String?

    init(_ string: String) { self.string = string } override var asynchronous: Bool { return true } ... override func start() { if cancelled { internalFinished = true return } internalExecuting = true let queue = dispatch_get_global_queue( QOS_CLASS_UTILITY, 0) dispatch_async(queue) { self.result = self.string.lowercaseString self.internalExecuting = false self.internalFinished = true } } }
  5. private var internalExecuting: Bool = false { willSet { willChangeValueForKey("isExecuting")

    } didSet { didChangeValueForKey("isExecuting") } } override var executing: Bool { return internalExecuting } private var internalFinished: Bool = false { willSet { willChangeValueForKey("isFinished") } didSet { didChangeValueForKey("isFinished") } } override var finished: Bool { return internalFinished }
  6. let waza = DragonOperation("Z") waza.completionBlock = { if let result

    = waza.result { println(result) // => "z" } } waza.start()
  7. let oraora = ["X", "Y", "Z"].map { DragonOperation($0) } let

    queue = NSOperationQueue() dispatch_async(dispatch_queue_create("Hissatsu", nil)) { queue.addOperations(oraora, waitUntilFinished: true) dispatch_sync(dispatch_get_main_queue()) { let results = oraora.flatMap { $0.result.map { [ $0 ] } ?? [] } println(results) // => ["x", "y", "z"] } }
  8. struct GeneratorOfNSEnumerator<T> : GeneratorType, SequenceType { private let enumerator: NSEnumerator

    init(_ enumerator: NSEnumerator) { self.enumerator = enumerator } mutating func next() -> T? { return self.enumerator.nextObject() as! T? } func generate() -> GeneratorOfNSEnumerator<T> { return GeneratorOfNSEnumerator<T>(self.enumerator) } } let fonts = "/System/Library/Fonts" let files = NSFileManager().enumeratorAtPath(fonts)! for path in GeneratorOfNSEnumerator<String>(files) { println(path) }
  9. class SharableTitle: NSObject, UIActivityItemSource { let title: String? init(title: String?)

    { self.title = title } func activityViewControllerPlaceholderItem( activityViewController: UIActivityViewController) -> AnyObject { return title ?? "" } func activityViewController( activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? { if activityType == UIActivityTypePostToTwitter || activityType == UIActivityTypePostToFacebook { return title } else { return nil } } func activityViewController( activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String { return title ?? "" } }
  10. let title = "Apple" let URL = NSURL(string: "http://www.apple.com/")! UIActivityViewController(

    activityItems: [SharableTitle(title: title), URL], applicationActivities: nil)
  11. class Awesome { func awesome(string: String) -> String { return

    string.uppercaseString } } let a = Awesome() Awesome.awesome(a)("Awesome") // => "AWESOME"
  12. class SuperAwesome: Awesome { override func awesome(string: String) -> String

    { return string.lowercaseString } } let s = SuperAwesome() Awesome.awesome(s)("Awesome") // => "awesome"