Skip to content

Instantly share code, notes, and snippets.

@geosava
Forked from zhangxigithub/copy.swift
Created October 12, 2015 21:22
Show Gist options
  • Save geosava/db9a7255522436ba0523 to your computer and use it in GitHub Desktop.
Save geosava/db9a7255522436ba0523 to your computer and use it in GitHub Desktop.
swift copy
import Cocoa
class Human:NSCopying
{
var name = ".."
var spouse:Human?
func copy() -> AnyObject! {
if let asCopying = ((self as AnyObject) as? NSCopying) {
return asCopying.copyWithZone(nil)
}
else {
assert(false, "This class doesn't implement NSCopying")
return nil
}
}
func copyWithZone(zone: NSZone) -> AnyObject! {
let newValue = Human()
newValue.name = name
newValue.spouse = spouse
return newValue
}
}
var h1 = Human()
h1.name
var h2 = h1.copy() as Human
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment