HomeiOS Developmentswift - Find out how to make a replica of UIView in...

swift – Find out how to make a replica of UIView in iOS 16


I used to be utilizing the under func to Make Copy of a UIView :

extension UIView {
    func copyObject<T:NSObject>() throws -> T? {
        let information = attempt NSKeyedArchiver.archivedData(withRootObject:self, requiringSecureCoding:false)
        return attempt NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(information) as? T
    }
}

Not too long ago I’ve up to date my app to help iOS 16. I get the under warning for the above code:

‘unarchiveObject(with:)’ was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: as an alternative

‘archivedData(withRootObject:)’ was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: as an alternative

After fixing the warning The code seems to be like this:

func copyView<T: UIView>() -> T {
    let information = attempt? NSKeyedArchiver.archivedData(withRootObject:self, requiringSecureCoding:false)
    return attempt! NSKeyedUnarchiver.unarchivedObject(ofClass: UIView.self, from:information!)  as! T
    //return NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: self)) as! T
}

Now when I attempt to copy, I get under error

error: Error Area=NSCocoaErrorDomain Code=4864 “This decoder will solely decode lessons that undertake NSSecureCoding. Class ‘UIView’ doesn’t undertake it.” UserInfo={NSDebugDescription=This decoder will solely decode lessons that undertake NSSecureCoding. Class ‘UIView’ doesn’t undertake it.}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments