Skip to content

Instantly share code, notes, and snippets.

@ikesyo
Last active May 20, 2017 06:52
Show Gist options
  • Save ikesyo/0c556e5fa116663583e2 to your computer and use it in GitHub Desktop.
Save ikesyo/0c556e5fa116663583e2 to your computer and use it in GitHub Desktop.
How to use Associated Object in Swift
import Foundation
class ABC {}
let abc = ABC()
// in-out expression can be used for CConstVoidPointer parameter.
var key: Void?
objc_setAssociatedObject(abc, &key, "value", UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
println(objc_getAssociatedObject(abc, &key)) // value
@ozgur
Copy link

ozgur commented Jul 4, 2015

Instead of

UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)

that would be a better fit

objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC

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