Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Last active November 23, 2015 18:39
Show Gist options
  • Select an option

  • Save zwaldowski/4388226c19dc7818e9f0 to your computer and use it in GitHub Desktop.

Select an option

Save zwaldowski/4388226c19dc7818e9f0 to your computer and use it in GitHub Desktop.
import Foundation
extension NSCopying {
func copied() -> Self {
let copied = copyWithZone(nil)
assert(copied is Self, "Invalid downcast while copying instance of \(Self.self), did you mean to use 'as?'")
return unsafeDowncast(copied)
}
}
extension Optional where Wrapped: NSCopying {
func copied() -> Wrapped? {
return map { $0.copied() }
}
}
let object: NSOrderedSet = [ 1, 2, 3, 4 ]
let copy = object.copied()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment