+ 4

New warnings on Swift 4 code from Sololearn curse with iOS 12.0

Taken from Sololearn course of Swift 4 fundamentals, are these 2 lines of code: let isSaved = NSKeyedArchiver.archiveRootObject(items, toFile: Item.ArchiveURL.path) return NSKeyedUnarchiver.unarchiveObject(withFile: Item.ArchiveURL.path!) as? [Item] It works fine now, but Xcode issues warnings on these lines: 'archiveRootObject(_:toFile:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead And 'unarchiveObject(withFile:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead The full aspect of the code is: class Item: NSObject, NSCoding { func encode(with aCoder: NSCoder) { aCoder.encode(name, forKey: "name") } required convenience init?(coder aDecoder: NSCoder) { let name = aDecoder.decodeObject(forKey: "name") as! String self.init(name: name) } init?(name: String) { self.name = name super.init() } } static let Dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! static let ArchiveURL = Dir.appendingPathComponent("items") func saveItems() { let isSaved = NSKeyedArchiver.archiveRootObject(items, toFile: Item.ArchiveURL.path) if !isSaved { print("Failed to save items...") } } func loadItems() -> [Item]? { return NSKeyedUnarchiver.unarchiveObject( withFile: Item.ArchiveURL.path!) as? [Item] } Anyone has any idea on how to implement the fixes of those warnings, so the code will continue to work on future versions? TIA

8th Oct 2018, 1:07 PM
Ronen Gil רונן גיל رونين جيل 🏳️‍🌈♾🇹🇩🇮🇱
Ronen Gil רונן גיל رونين جيل 🏳️‍🌈♾🇹🇩🇮🇱 - avatar
2 Réponses
0
I’m sorry, but I can’t find an answer to the question there...
19th Nov 2018, 2:55 PM
Ronen Gil רונן גיל رونين جيل 🏳️‍🌈♾🇹🇩🇮🇱
Ronen Gil רונן גיל رونين جيل 🏳️‍🌈♾🇹🇩🇮🇱 - avatar