Skip to content
 
 

Latest commit

 

History

40 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ClosureKit

Closures make code clear and readable. You can write self-contained, small snipets of code instead of having the logic spread throughout your app, Cocoa is moving slowly to a block/closure approach but there are still a lot of Cocoa libraries (such as UIKit) that don't support Closures. ClosureKit hopes to facilitate this kind of programming by removing some of the annoying - and, in some cases, impeding - limits on coding with closures.

Requirements

  • iOS 7.0+
  • Xcode 8.3+

Installation

CocoaPods is a dependency manager for Cocoa projects.

CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:

$ gem install cocoapods

To integrate ClosureKit into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'ClosureKit'

Then, run the following command:

$ pod install

Usage

UIControl

Closure control event handling for UIControl

let button = UIButton.buttonWithType(.System) as! UIButton
button.addEventHandler(forControlEvents: .TouchUpInside) { button in
    println("Button touched!!! \(button)")
}

UIGestureRecognizer

Closure functionality for UIGestureRecognizer.

let doubleTap = UITapGestureRecognizer { gesture, state in
    println("Double tap!")
}
doubleTap.numberOfTapsRequired = 2
self.addGestureRecognizer(doubleTap)

UIWebView

Closure support for UIWebView delegate.

let webView = UIWebView()
webView.shouldStartLoad = { webView, request, type in
    println("shouldStartLoad: \(request)")
    return true
}

webView.didStartLoad = { webView in
    println("didStartLoad: \(webView)")
}

webView.didFinishLoad = { webView in
    println("didFinishLoad \(webView)")
}

webView.didFinishWithError = { webView, error in
    println("didFinishWithError \(error)")
}

UIImagePickerController

UIImagePickerController with closure callback(s).

let picker = UIImagePickerController()
picker.didCancel = { picker in
    println("DID CANCEL! \(picker)")
}
picker.didFinishPickingMedia = { picker, media in 
    println("Media: \(media[UIImagePickerControllerEditedImage])")
}
self.presentViewController(picker, animated: true, completion: nil)

NSObject

Closure wrapper for key-value observation.

In Mac OS X Panther, Apple introduced an API called "key-value observing." It implements an observer pattern, where an object will notify observers of any changes in state. NSNotification is a rudimentary form of this design style; KVO, however, allows for the observation of any change in key-value state. The API for key-value observation, however, is flawed, ugly, and lengthy.

Like most of the other closure abilities in ClosureKit, observation saves and a bunch of code and a bunch of potential bugs.

WARNING: Observing using closures and cocoa observers are independant. Meaning that you shouldn't add a "traditional" observer and then remove it using this wrapper nor add a closure observer and remove it using Cocoa methods.

self.observeKeyPath("testing", options: .New | .Old) { newValue, oldValue in
    println("Property was: \(oldValue), now is: \(newValue)")
}

MFMailComposeViewController

MFMailComposeViewController with closure callback.

Note that when setting a completion handler, you don't have the responsability to dismiss the view controller anymore.

let composeViewController = MFMailComposeViewController { viewController, result, type in println("Done") }
composerViewController.setSubject("Test")

MFMessageComposeViewController

MFMessageComposeViewController with closure callback.

Note that when setting a completion handler, you don't have the responsability to dismiss the view controller anymore.

let composeViewController = MFMessageComposeViewController { viewController, result in println("Done") }
composerViewController.body = "test sms"

UIBarButtonItem

Closure event initialization for UIBarButtonItem.

self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .Bordered) { btn in
    println("Button touched!!!!!! \(btn)")
}

CADisplayLink

CADisplayLink closures implementation.

CADisplayLink.runFor(5.0) { progress in
    println("Awesome \(progress * 100)%")
}

CLLocationManager

Closure implementation of CLLocationManager delegate.

Note that when using startUpdatingLocation(handler) you need to use the counterpart stopUpdatingLocationHandler or you'll leak memory.

Example:

let locationManager = CLLocationManager()
locationManager.starUpdatingLocation { location in
    println("Location: \(location)")
}
locationManager.stopUpdatingLocationHandler()

WARNING: You cannot use closures and set a delegate at the same time. Setting a delegate will prevent closures for being called and setting a closure will overwrite the delegate property.

NSTimer

Simple closure implementation on NSTimer scheduling.

NSTimer.scheduledTimerWithTimeInterval(1.0, repeats: false) { timer in
    println("Did something after 1s!")
}

WARNING: You cannot use closures and set a delegate at the same time. Setting a delegate will prevent closures for being called and setting a closure will overwrite the delegate property.

Authors

Martín Conte Mac Donell @fz

About

Closures on most used UIKit methods

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages