Created
May 20, 2016 10:52
-
-
Save aufflick/5dc5029d0af41ddc21adcceb075536c1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
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