+ 3

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?

29th Jan 2019, 12:44 PM
Patryk Piwowarczyk
Patryk Piwowarczyk - avatar
1 Antwort
0
Try contact sololearn
7th Feb 2019, 7:08 AM
Gavrie Sashenka
Gavrie Sashenka - avatar