Skip to content

Instantly share code, notes, and snippets.

@ikesyo
Last active May 20, 2017 06:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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