Skip to content

Instantly share code, notes, and snippets.

@christopher-fuller
Created November 4, 2016 19:15
Show Gist options
  • Save christopher-fuller/591b38de8355e39ec10c1c175e56325b to your computer and use it in GitHub Desktop.
Save christopher-fuller/591b38de8355e39ec10c1c175e56325b to your computer and use it in GitHub Desktop.
//
// ObjectAssociable.swift
// Created by Christopher Fuller for Southern California Public Radio
// Original version at https://github.com/SCPR/swift-experiments/blob/master/source/ObjectAssociable.swift
// Updated by Christopher Fuller for Swift 3 compatibility
//
// Copyright (c) 2016 Southern California Public Radio
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
protocol ObjectAssociable: AnyObject {}
extension ObjectAssociable {
func associatedObject<T>(key: UnsafeRawPointer) -> T? {
return objc_getAssociatedObject(self, key) as? T
}
func associateObject<T>(_ object: T?, key: UnsafeRawPointer) {
objc_setAssociatedObject(self, key, object, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
func dissociateObject(key: UnsafeRawPointer) {
associateObject((nil as Any?), key: key)
}
func dissociateObjects() {
objc_removeAssociatedObjects(self)
}
}
@christopher-fuller
Copy link
Author

Example Usage

class Example {}

extension Example: ObjectAssociable {

    private struct Key {
        static var dateA = 0
        static var dateB = 0
    }

    var dateA: Date? { // Date is just for example, can be any type.
        get {
            return associatedObject(key: &Key.dateA)
        }
        set {
            associateObject(newValue, key: &Key.dateA)
        }
    }

    var dateB: Date? { // Date is just for example, can be any type.
        get {
            return associatedObject(key: &Key.dateB)
        }
        set {
            associateObject(newValue, key: &Key.dateB)
        }
    }

    func removeDateA() {
        dissociateObject(key: &Key.dateA)
    }

    func removeDateB() {
        dissociateObject(key: &Key.dateB)
    }

    func removeDates() {
        dissociateObjects()
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment