Latest 2.0.0
Homepage https://github.com/tokorom/HasAssociatedObjects
License MIT
Platforms ios 8.0, tvos 9.0
Authors

==================

Swift Version
License
CocoaPods Compatible
Carthage Compatible

We can add some stored objects to Swift extension

Q. Can we add a stored property to extension?

A. No, but we can use assciated objects instead.

Simple Usage

extension UIViewController: HasAssociatedObjects {

    // new stored property
    var storedString: String? {
        get {
            return associatedObjects.value as? String
        }
        set {
            associatedObjects.value = newValue
        }
    }

}

Other Usages

Multiple stored properties

extension UIViewController: HasAssociatedObjects {

    var storedString: String? {
        get {
            return associatedObjects["STRING"] as? String
        }
        set {
            associatedObjects["STRING"] = newValue ?? ""
        }
    }

    var storedInt: Int {
        get {
            guard let value = associatedObjects["INT"] as? Int else {
                return 0 //< default value
            }
            return value
        }
        set {
            associatedObjects["INT"] = newValue
        }
    }

}

Use directly

extension Subject: HasAssociatedObjects {
}

subject.associatedObjects.value = 10

let storedValue = subject.associatedObjects.value as? Int
XCTAssertEqual(10, storedValue)

Subject is not AnyObject

// subject is struct
struct AnySubject {
    let identifier: Int
}

// a appropriate property that can be cleaned
var propertyOfSomeone: [Int: AssociatedObjects] = [:]

// You can customize `associatedObjects`
extension AnySubject: HasAssociatedObjects {
    var associatedObjects: AssociatedObjects {
        guard let associatedObjects = propertyOfSomeone[hashValue] else {
            let associatedObjects = AssociatedObjects()
            propertyOfSomeone[hashValue] = associatedObjects
            return associatedObjects
        }
        return associatedObjects
    }
}

extension AnySubject: Hashable {
    var hashValue: Int {
        return identifier
    }

    static func == (lhs: AnySubject, rhs: AnySubject) -> Bool {
        return lhs.hashValue == rhs.hashValue
    }
}

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

You can install it with the following command:

$ gem install cocoapods

To integrate HasAssociatedObjects 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 'HasAssociatedObjects'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate HasAssociatedObjects into your Xcode project using Carthage, specify it in your Cartfile:

github "tokorom/HasAssociatedObjects"

Then, run the following command:

$ carthage update

Then, link your app with HasAssociatedObjects.framework.

Latest podspec

{
    "name": "HasAssociatedObjects",
    "version": "2.0.0",
    "summary": "We can add some stored objects to Swift extension",
    "homepage": "https://github.com/tokorom/HasAssociatedObjects",
    "license": "MIT",
    "authors": {
        "tokorom": "[email protected]"
    },
    "source": {
        "git": "https://github.com/tokorom/HasAssociatedObjects.git",
        "tag": "2.0.0"
    },
    "platforms": {
        "ios": "8.0",
        "tvos": "9.0"
    },
    "source_files": "HasAssociatedObjects/**/*.swift",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This