Skip to content

Instantly share code, notes, and snippets.

@aufflick
Created May 20, 2016 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aufflick/5dc5029d0af41ddc21adcceb075536c1 to your computer and use it in GitHub Desktop.
Save aufflick/5dc5029d0af41ddc21adcceb075536c1 to your computer and use it in GitHub Desktop.
/*
get {
return (objc_getAssociatedObject(self, &ourObserverItemKey)
as? AssociatedStruct<SomeStructType>)
.map {$0.value}
}
set {
objc_setAssociatedObject(self,
&ourObserverItemKey,
newValue.map
{ AssociatedStruct<SomeStructType>($0) },
.OBJC_ASSOCIATION_RETAIN)
}
*/
public final class AssociatedStruct<T>: NSObject, NSCopying {
public typealias Type = T
public let value: Type
public init(_ value: Type) { self.value = value }
public func copyWithZone(zone: NSZone) -> AnyObject {
return self.dynamicType.init(value)
}
}
extension AssociatedStruct where T: NSCopying {
public func copyWithZone(zone: NSZone) -> AnyObject {
return self.dynamicType.init(value.copyWithZone(zone) as! Type)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment