archiveRootObject(_:toFile:) deprecated

Hello,

I am new using NSCoding protocol. I understand how it works, but I don't know how to save and load the data. I've tried with NSKeyedArchiver.archiveRootObject(_:toFile:) and NSKeyedUnarchiver.unarchiveObject(withFile:), but I get a warning saying that they were deprecated. What method should I use?

Thank you!

Accepted Reply

You can use `NSKeyedArchiver.archivedData(withRootObject:requiringSecureCoding:)`, as suggested in the deprecation warning of `NSKeyedArchiver.archiveRootObject(_:toFile:)`.

(You may need to store the archived data.)


Also, `NSKeyedUnarchiver.unarchivedObject(ofClass:from:)` or `NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(_:)` for unarchiving.

Replies

You can use `NSKeyedArchiver.archivedData(withRootObject:requiringSecureCoding:)`, as suggested in the deprecation warning of `NSKeyedArchiver.archiveRootObject(_:toFile:)`.

(You may need to store the archived data.)


Also, `NSKeyedUnarchiver.unarchivedObject(ofClass:from:)` or `NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(_:)` for unarchiving.

I am new using

NSCoding
protocol.

Are you sure you should be using

NSCoding
at all. Most Swift programs use Swift’s
Codable
instead. There are good reasons to using
NSCoding
— for example, to archive an object graph with non-trivial connectivity — but if you don’t need to handle such cases then
Codable
is much easier.

Also, if you decide to stick with

NSCoding
, you really need to look at
NSSecureCoding
. All the standard uses cases for
NSCoding
can benefit from the security provided by
NSSecureCoding
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"