Create a copy of a UIView in Swift

后端 未结 7 1845
长发绾君心
长发绾君心 2020-11-29 05:01

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

相关标签:
7条回答
  • 2020-11-29 05:49

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题