0

I was using the below func to Make Copy of a UIView :

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

Recently I have updated my app to support iOS 16. I am getting the below warning for the above code:

'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead

'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead

After fixing the warning The code looks like this:

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

Now when I try to copy, I am getting below error

error: Error Domain=NSCocoaErrorDomain Code=4864 "This decoder will only decode classes that adopt NSSecureCoding. Class 'UIView' does not adopt it." UserInfo={NSDebugDescription=This decoder will only decode classes that adopt NSSecureCoding. Class 'UIView' does not adopt it.}

HangarRash
  • 7,314
  • 5
  • 5
  • 32
tarun5130
  • 55
  • 7
  • 1
    This seems like an [XY problem](https://xyproblem.info). Why are you trying to copy views in the first place? – Sweeper Jul 20 '23 at 06:25
  • I want to pass the copied view to a third party to generate a PDF file from this view – tarun5130 Jul 20 '23 at 07:09
  • Why do you need a *copy* then? Does the third party modify the view in some way? And why can't you just run the code you used to create the view a second time? – Sweeper Jul 20 '23 at 07:12
  • Yes, the third party is modifying the view it is adding extra blank spaces at the bottom. Till now the above code was working fine but once I updated my app to iOS16, I had to update the deployment target to iOS13 & I ran into this issue. – tarun5130 Jul 20 '23 at 07:18
  • *That* sounds like your actual problem. I'd suggest that you [edit] your question to show a [mcve] of that problem instead. – Sweeper Jul 20 '23 at 07:22
  • I have taken a reference from https://stackoverflow.com/a/52927098/12797715, but it seems to be no longer working in iOS16. I have a similar use case. – tarun5130 Jul 20 '23 at 07:29

0 Answers0