Last active
May 20, 2017 06:52
-
-
Save ikesyo/0c556e5fa116663583e2 to your computer and use it in GitHub Desktop.
How to use Associated Object in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of
UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)
that would be a better fit
objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC