XCode Caution: 'archiveRootObject(_:toFile:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: and -writeToURL:options:error: instead

Hello, I am receiving this caution, is anyone able to fix this please?

Thank you in advance!

    if let filePath = pathForItems() {
      NSKeyedArchiver.archiveRootObject(items, toFile: filePath)
       
      // Post Notification
      NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ShoppingListDidChangeNotification") , object: self)
    }
  }
Answered by Claude31 in 733555022

Here is usual code:

You should create the url

        let fileURL = // Your url
        do {
            let data = try NSKeyedArchiver.archivedData(withRootObject: items, requiringSecureCoding: true)
            try data.write(to: fileURL)
        } catch {
            // You may need some better error handling...
        }
Accepted Answer

Here is usual code:

You should create the url

        let fileURL = // Your url
        do {
            let data = try NSKeyedArchiver.archivedData(withRootObject: items, requiringSecureCoding: true)
            try data.write(to: fileURL)
        } catch {
            // You may need some better error handling...
        }
XCode Caution: 'archiveRootObject(_:toFile:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: and -writeToURL:options:error: instead
 
 
Q