Because objects are reference types, not value types, if you set a UIView
equal to another UIView
, the views are the same object. If you modify one
Update for iOS 12.0
Methods archivedData(withRootObject:)
and unarchivedObject(with:)
are deprecated as of iOS 12.0.
Here is an update to @Ivan Porcolab's answer using the newer API (since 11.0), also made more general to support other types.
extension NSObject {
func copyObject<T:NSObject>() throws -> T? {
let data = try NSKeyedArchiver.archivedData(withRootObject:self, requiringSecureCoding:false)
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? T
}
}