Latest 3.0.0
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0

SwiftHook is a simple and key-based callback library written in Swift.

Check out the generated docs at


SwiftHook is a pure Swift project and supports Swift Package Manager, Carthage and CocoaPods.

Swift Package Manager

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .package(url: "", from: "3.0.0")


github "vknabel/SwiftHook" ~> 3.0.0


source ''

pod 'SwiftHook', '~> 3.0.0'


A HookEvent is a wrapper around a raw value that adds some additional type information.
This type information gives callbacks compile-time safety.

/// example for DelegationHook
enum RepositoryPolicy: HookAction {
  case nameAllowed
/// example for SerialHook
enum RepositoryAction: HookAction {
  case nameChanged

extension HookEvent {
  static let nameAllowed: HookEvent<RepositoryPolicy, String, Bool> = HookEvent(action: .nameAllowed)
  static let nameChanged: HookEvent<RepositoryAction, String, ()> = HookEvent(action: .nameChanged)


A DelegationHook only supports one single callback for each hook key and can be used instead of small delegates. Added closures are stored retained.

var hook = DelegationHook<RepositoryPolicy>()
hook.respond(to: .nameAllowed) { string in
  let len = count(string)
  return len > 1 && len < 15
let allowed = hook.trigger(event: .nameAllowed, with: "SwiftHook")
  .reduce(into: true, { $0 && $1 })


A SerialHooksupports multiple closures per HookEvent, but respond(to:, with:) returns an optional reference to be stored in a strong reference.

var hook = SerialHook<RepositoryAction>()
var updateObject = hook.respond(to: .nameChanged) { name in /*update title*/ }
var update2Object = hook.respond(to: .nameChanged) { name in /*update title in another place*/ }
hook.trigger(event: .nameChanged, with: "NewName")
updateObject = nil // removes the first name changed closure


SwiftHook is released unter MIT License.

Latest podspec

    "name": "SwiftHook",
    "version": "3.0.0",
    "summary": "A simple and key-based callback library written in Swift.",
    "description": "SwiftHook is a simple and key-based callback library written in Swift.",
    "social_media_url": "",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Valentin Knabel": "[email protected]"
    "source": {
        "git": "",
        "tag": "3.0.0"
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    "source_files": "Sources/SwiftHook/*.swift"

Pin It on Pinterest

Share This