Wednesday, January 21, 2015

Associated Objects in Swift

Associated Objects are a great way to put properties on classes that you do not control.

For NSObject Subclasses

You must use this method for String and Array, because they are not classes: they're structs. The toll-free bridging to NSObject subclasses allows this to work.

For Custom Objects

Here it's a lot simpler, but since AnyObject is a protocol, you have to use an actual class. This definitely limits the utility of associated objects. My much longer, rambling post on Objective-C and Associated Objects.

No comments: