NSKeyedUnarchiver not returning the "nil" - ios

Good day,
I am on the verge of finishing the tutorial app from Code School, and I was really playing around with its archiving and unarchiving the data "to and with a file". So essentially the archiving works, which is the code below.
class func saveOrdersToArchive(cart: Orders) -> Bool {
print(archiveFilePath());
return NSKeyedArchiver.archiveRootObject(cart, toFile: archiveFilePath());
}
The the archiveFilePath() function is implemented this way, it basically creates a file called "cart.archive" and stores it on the simulator's local drive.
class func archiveFilePath() -> String {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
return documentsDirectory.appendingPathComponent("cart.archive").path;
}
So it creates the file correctly then stores the data of type Orders.
But when I try to retrieve the data with my implementation below, it seems that the returned data is marked as "nil".
class func readOrdersFromArchive() -> Orders? {
print(archiveFilePath());
return NSKeyedUnarchiver.unarchiveObject(withFile: archiveFilePath()) as? Orders
}
So, in the main ViewController file, the saving of the Object is implemented below.
//name
productNames = ["1907 Wall set", "1921 Dial phone"];
//cell Images
productImages = [ #imageLiteral(resourceName: "image-cell1"), #imageLiteral(resourceName: "image-cell2")];
//phone Images
phoneImages = [#imageLiteral(resourceName: "phone-fullscreen1"), #imageLiteral(resourceName: "phone-fullscreen2")];
//price
priceProducts = [1.99, 3.99]
oCartProducts = Product(names: productNames, productImages: productImages, cellImages: phoneImages, priceOfProducts: priceProducts);
order = Orders(order_id: 1, orders: oCartProducts);
print(Orders.saveOrdersToArchive(cart: order));
The function prints to true, to indicate successful archive.
The implementation for retrieving the data is implemented below,
if let order1 = Orders.readOrdersFromArchive(){
order = order1
if let o = order.orders{
if let n = o.names{
print(n.count)
}
}
}
The reason I want to print the "count" is to be able to make sure the unwrapped object has values, but the code doesn't go there meaning the object is nil.
I am doing so init in the ViewController before storing the variables as follows,
var oCartProducts = Product(names: [String](), productImages: [UIImage](), cellImages: [UIImage](), priceOfProducts: [Double]());
var order = Orders(order_id: Int(), orders: Product(names: [String](), productImages: [UIImage](), cellImages: [UIImage](), priceOfProducts: [Double]()));
Showing Orders Class,
class Orders : NSObject, NSCoding{
var order_id: Int?
var orders: Product?
init(order_id: Int?, orders: Product?){
self.order_id = order_id;
self.orders = orders;
}
required init?(coder aDecoder: NSCoder) {
self.orders = aDecoder.decodeObject(forKey: "orders") as? Product
self.order_id = aDecoder.decodeInteger(forKey: "order_id")
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.order_id);
aCoder.encode(self.orders);
}
class func archiveFilePath() -> String {
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
return documentsDirectory.appendingPathComponent("cart.archive").path;
}
class func readOrdersFromArchive() -> Orders? {
print(archiveFilePath());
return NSKeyedUnarchiver.unarchiveObject(withFile: archiveFilePath()) as? Orders
}
class func saveOrdersToArchive(cart: Orders) -> Bool {
print(archiveFilePath());
return NSKeyedArchiver.archiveRootObject(cart, toFile: archiveFilePath());
}
Showing Product Class,
class Product: NSObject, NSCoding {
var names: [String]?
var productImages: [UIImage]?
var cellImages: [UIImage]?
var priceOfProducts: [Double]?
init(names: [String]?, productImages: [UIImage]?, cellImages: [UIImage]?, priceOfProducts: [Double]?) {
self.names = names;
self.productImages = productImages;
self.cellImages = cellImages;
self.priceOfProducts = priceOfProducts;
}
required init?(coder aDecoder: NSCoder) {
self.names = aDecoder.decodeObject(forKey: "names") as? [String];
self.productImages = aDecoder.decodeObject(forKey: "productNames") as? [UIImage];
self.cellImages = aDecoder.decodeObject(forKey: "cellImages") as? [UIImage];
self.priceOfProducts = aDecoder.decodeObject(forKey: "priceOfProducts") as? [Double];
}
func encode(with aCoder: NSCoder) {
aCoder.encode(self.names);
aCoder.encode(self.productImages);
aCoder.encode(self.cellImages);
aCoder.encode(self.priceOfProducts);
}
}
Hope you can shed some light.

Show us your Orders class. Does it conform to NSCoding?
func encode(with aCoder: NSCoder) {
if let id = self.order_id {
aCoder.encode(self.order_id, forKey: "order_id")
}
if let orders = self.orders {
aCoder.encode(self.orders, forKey: "orders")
}
}

Related

NSUserDefaults returns empty array

I have a class Person, which is a model
import Foundation
public class Person : NSObject , NSCoding{
public var name:String="";
public var gender:Int = 0;
public var status:Int = 0
override init (){
super.init()
}
public init (name:String, gender:Int){
self.name=name;
self.gender=gender;
}
required public init(coder aDecoder: NSCoder) { }
public func encodeWithCoder(_aCoder: NSCoder) { }
}
Now when I try to retreive [Person] array I use method getPersons(arr : String!) . If there is no data in NSUserDefaults , I create it, put 1 element into it and store using setPersons()method.
The problem is that on retrieving I get an array with correct count (1), but the object is not filled, having "" in String and 0 in Int variables.
private func archivePeople (people : [Person]) -> NSData{
return NSKeyedArchiver.archivedDataWithRootObject(people as NSArray)
}
public func getPersons(arr : String!) -> [Person]{
var array : [Person] = []
if let unarchivedObject = NSUserDefaults.standardUserDefaults().objectForKey(arr) as? NSData {
array = (NSKeyedUnarchiver.unarchiveObjectWithData(unarchivedObject) as? [Person])!
}
if array.count == 0 {
array.append(Person(name: A.DEFAULT_THEIR_NAMES,gender: 0))
setPersons(arr, people: array)
}
return array
}
public func setPersons(key: String, people : [Person]){
let archivedObject = archivePeople(people)
userDefaults.setObject(archivedObject, forKey: key)
userDefaults.synchronize()
}
What is wrong? Do I set or retrieve this in a wrong way?
You need to override properly the NSCoding functions.
It should look like something the code below:
required public convenience init(coder aDecoder: NSCoder) {
let name: String = aDecoder.decodeObject(forKey: "name") as! String
let gender = aDecoder.decodeInt64(forKey: "gender")
let status = aDecoder.decodeInt64(forKey: "status")
self.init(name: name, gender: Int(gender))
self.status = Int(status)
}
public func encodeWithCoder(_aCoder: NSCoder) {
_aCoder.encode(self.name, forKey: "name")
_aCoder.encode(Int64(self.gender), forKey: "gender")
_aCoder.encode(Int64(self.status), forKey: "status")
}

Swift, NSCoding to save array of a class not working

I'm new in Swift and iOS and I came across this issue while making an app. I want to basically store user data using NSCoding onto local storage. However, my code below doesn't do it. Anyone can tell me what's wrong with it? Much appreciated!
Also, both saveChecklist and LoadChecklist are called in appDelegate, under applicationDidEnterBackground and applicationWillTerminate.
I have a feeling my issue lies in encodeWithCoder and init(aDecoder), as I used GET to append my Checklist item into lists.
My code in DataModel.class:
import Foundation
class DataModel: NSObject, NSCoding {
var checklist = Checklist()
var lists: [Checklist] {
get {
return [checklist]
}
set {
}
}
override init() {
super.init()
loadChecklist()
}
// MARK: - All the saving stuff
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(checklist, forKey: "Checklist")
}
required init?(coder aDecoder: NSCoder) {
checklist = aDecoder.decodeObjectForKey("Checklist") as! Checklist
}
func documentsDirectory() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
return paths[0]
}
func dataFilePath() -> String {
return (documentsDirectory() as NSString).stringByAppendingPathComponent("Checklist.plist") // create file if no checklist.plist is present
}
func saveChecklist() {
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWithMutableData: data)
archiver.encodeObject(lists, forKey: "Checklists")
archiver.finishEncoding()
data.writeToFile(dataFilePath(), atomically: true)
}
func loadChecklist() {
let path = dataFilePath()
if NSFileManager.defaultManager().fileExistsAtPath(path) {
if let data = NSData(contentsOfFile: path) {
let unarchiver = NSKeyedUnarchiver(forReadingWithData: data)
lists = unarchiver.decodeObjectForKey("Checklists") as! [Checklist]
unarchiver.finishDecoding()
}
}
}
My code in Checklist.class:
import Foundation
class Checklist: NSObject, NSCoding {
var item = [items]()
var rituals = [items]()
var doneButtonVisible: Bool
var streak: Int
var itemDoneCount: Int
var startDate: NSDate
var dayHasStarted: Bool
override init() {
doneButtonVisible = false
itemDoneCount = 0
streak = 0
startDate = NSDate()
dayHasStarted = false
super.init()
}
// saves
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(item, forKey: "Items")
aCoder.encodeObject(rituals, forKey: "Rituals")
aCoder.encodeObject(itemDoneCount, forKey: "ItemDoneCount")
aCoder.encodeObject(doneButtonVisible, forKey: "DoneButtonVisible")
aCoder.encodeObject(streak, forKey: "Streak")
aCoder.encodeObject(startDate, forKey: "StartDate")
aCoder.encodeObject(dayHasStarted, forKey: "DayHasStarted")
}
// loads
required init?(coder aDecoder: NSCoder) {
item = aDecoder.decodeObjectForKey("Items") as! [items]
rituals = aDecoder.decodeObjectForKey("Rituals") as! [items]
itemDoneCount = aDecoder.decodeObjectForKey("ItemDoneCount") as! Int
doneButtonVisible = aDecoder.decodeObjectForKey("DoneButtonVisible") as! Bool
streak = aDecoder.decodeObjectForKey("Streak") as! Int
startDate = aDecoder.decodeObjectForKey("StartDate") as! NSDate
dayHasStarted = aDecoder.decodeObjectForKey("DayHasStarted") as! Bool
super.init()
}
}
UPDATE
Solved the saving and loading issue by revamping my data model. Using Core Data instead to save and load data.
However, I still have no idea as to why the above code does not load. Apparently, it does not save the contents of checklist to begin with.
Your Checklist class needs to inherit from NSCoding too, and implement the encodeWithCoder(coder: NSCoder) method. This is a good tutorial on NSCoding.

Achieving data persistence for an array of custom type [duplicate]

I am trying to use NSUserDefaults to save an array in to my app's core data. I thought it would be good to use NSUserDefaults but the problem is that wherever I put the code that creates the default it throws up the SIGABRT error.
Here is the code that creates the default:
let levelArrayDefault = NSUserDefaults.standardUserDefaults()
levelArrayDefault.setValue(levelsArray, forKey: "levelsArray")
levelArrayDefault.synchronize()
levelsArray is an array of List objects:
class List: NSObject, NSCoding {
// MARK: Properties
var name: String
var AnswersArray = [Answer]()
init?(name: String) {
// Initialize stored properties.
self.name = name
if name.isEmpty {
return nil
}
}
required init(coder decoder: NSCoder){
self.AnswersArray = (decoder.decodeObjectForKey("AA") as? [Answer])!
self.name = (decoder.decodeObjectForKey("name") as? String)!
}
func encodeWithCoder(coder: NSCoder) {
if let AnswersArray = AnswersArray { coder.encodeObject(AnswersArray, forKey: "AA") }
if let name = name { coder.encodeObject(name, forKey: "name") }
}
}
class Answer: NSObject, NSCoding {
var EnglishAnswer: String = ""
var ChineseAnswer: String = ""
init(newEng: String, newChi: String){
self.EnglishAnswer = newEng
self.ChineseAnswer = newChi
}
required init(coder decoder: NSCoder){
self.EnglishAnswer = (decoder.decodeObjectForKey("EnglishAnswer") as? String)!
self.ChineseAnswer = (decoder.decodeObjectForKey("ChineseAnswer") as? String)!
}
func encodeWithCoder(coder: NSCoder) {
if let EnglishAnswer = EnglishAnswer { coder.encodeObject(EnglishAnswer, forKey: "EnglishAnswer") }
if let ChineseAnswer = ChineseAnswer { coder.encodeObject(ChineseAnswer, forKey: "ChineseAnswer") }
}
}
How can I stop SIGABRT from popping up and get the array to be stored.
Help would be much appreciated.
You need to convert it to NSData using NSKeyedArchiver before storing it to NSUserDefaults, try like this:
update: Xcode 11.4 • Swift 5.2 or later
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let list = List(name: "Student")
list.answers = [Answer(english: "english answer", chinese: "中文回答")]
let data = (try? NSKeyedArchiver.archivedData(withRootObject: [list], requiringSecureCoding: false)) ?? Data()
UserDefaults.standard.set(data, forKey: "listData")
guard
let loadedData = UserDefaults.standard.data(forKey: "listData"),
let loadedArray = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(loadedData) as? [List]
else { return }
print(loadedData.count)
print(loadedArray.first ?? "none")
print(loadedArray.first?.name ?? "no name")
print(loadedArray.first?.answers.first?.english ?? "no english")
print(loadedArray.first?.answers.first?.chinese ?? "no chinese")
}
}
class Answer: NSObject, NSCoding {
let english: String
let chinese: String
init(english: String, chinese: String) {
self.english = english
self.chinese = chinese
}
required init(coder decoder: NSCoder) {
self.english = decoder.decodeString(forKey: "english")
self.chinese = decoder.decodeString(forKey: "chinese")
}
func encode(with coder: NSCoder) {
coder.encode(english, forKey: "english")
coder.encode(chinese, forKey: "chinese")
}
}
class List: NSObject, NSCoding {
let name: String
fileprivate var data = Data()
var answers: [Answer] {
get {
(try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)) as? [Answer] ?? []
}
set {
data = (try? NSKeyedArchiver.archivedData(withRootObject: newValue, requiringSecureCoding: false)) ?? Data()
}
}
init(name: String) {
self.name = name
}
required init(coder decoder: NSCoder) {
self.data = decoder.decodeData(forKey: "answersData")
self.name = decoder.decodeString(forKey: "name")
}
func encode(with coder: NSCoder) {
coder.encode(data, forKey: "answersData")
coder.encode(name, forKey: "name")
}
}
extension NSCoder {
func decodeString(forKey key: String) -> String {
return decodeObject(forKey: key) as? String ?? ""
}
func decodeData(forKey key: String) -> Data {
return decodeObject(forKey: key) as? Data ?? Data()
}
}
If you want to save your custom object in NSUserDefaults, it's not enough to make your class NSCoding-compliant -- you have to actually encode the data into an NSData object. This is a common mistake -- see my answer to another question for a similar situation.
So, you've added NSCoding to your Answer and List classes. That's a good start. Before you continue, you should verify that you've got that step right by using a NSKeyedArchiver to encode an example of a List object containing a few Answer objects into an instance of NSData, and then use NSKeyedUnarchiver to decode that data object back into your List. Verify that everything that you care about completes the round trip with no problems. This would be an excellent place to use Xcode's testing facility -- you could write a unit test that does exactly what I've described.
Once you know you've got the NSCoding stuff right, you should modify your code so that it encodes your List as NSData and stores the resulting data object in NSUserDefaults using the -setObject:forKey: method.

Using NSUserDefaults on arrays

I am trying to use NSUserDefaults to save an array in to my app's core data. I thought it would be good to use NSUserDefaults but the problem is that wherever I put the code that creates the default it throws up the SIGABRT error.
Here is the code that creates the default:
let levelArrayDefault = NSUserDefaults.standardUserDefaults()
levelArrayDefault.setValue(levelsArray, forKey: "levelsArray")
levelArrayDefault.synchronize()
levelsArray is an array of List objects:
class List: NSObject, NSCoding {
// MARK: Properties
var name: String
var AnswersArray = [Answer]()
init?(name: String) {
// Initialize stored properties.
self.name = name
if name.isEmpty {
return nil
}
}
required init(coder decoder: NSCoder){
self.AnswersArray = (decoder.decodeObjectForKey("AA") as? [Answer])!
self.name = (decoder.decodeObjectForKey("name") as? String)!
}
func encodeWithCoder(coder: NSCoder) {
if let AnswersArray = AnswersArray { coder.encodeObject(AnswersArray, forKey: "AA") }
if let name = name { coder.encodeObject(name, forKey: "name") }
}
}
class Answer: NSObject, NSCoding {
var EnglishAnswer: String = ""
var ChineseAnswer: String = ""
init(newEng: String, newChi: String){
self.EnglishAnswer = newEng
self.ChineseAnswer = newChi
}
required init(coder decoder: NSCoder){
self.EnglishAnswer = (decoder.decodeObjectForKey("EnglishAnswer") as? String)!
self.ChineseAnswer = (decoder.decodeObjectForKey("ChineseAnswer") as? String)!
}
func encodeWithCoder(coder: NSCoder) {
if let EnglishAnswer = EnglishAnswer { coder.encodeObject(EnglishAnswer, forKey: "EnglishAnswer") }
if let ChineseAnswer = ChineseAnswer { coder.encodeObject(ChineseAnswer, forKey: "ChineseAnswer") }
}
}
How can I stop SIGABRT from popping up and get the array to be stored.
Help would be much appreciated.
You need to convert it to NSData using NSKeyedArchiver before storing it to NSUserDefaults, try like this:
update: Xcode 11.4 • Swift 5.2 or later
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let list = List(name: "Student")
list.answers = [Answer(english: "english answer", chinese: "中文回答")]
let data = (try? NSKeyedArchiver.archivedData(withRootObject: [list], requiringSecureCoding: false)) ?? Data()
UserDefaults.standard.set(data, forKey: "listData")
guard
let loadedData = UserDefaults.standard.data(forKey: "listData"),
let loadedArray = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(loadedData) as? [List]
else { return }
print(loadedData.count)
print(loadedArray.first ?? "none")
print(loadedArray.first?.name ?? "no name")
print(loadedArray.first?.answers.first?.english ?? "no english")
print(loadedArray.first?.answers.first?.chinese ?? "no chinese")
}
}
class Answer: NSObject, NSCoding {
let english: String
let chinese: String
init(english: String, chinese: String) {
self.english = english
self.chinese = chinese
}
required init(coder decoder: NSCoder) {
self.english = decoder.decodeString(forKey: "english")
self.chinese = decoder.decodeString(forKey: "chinese")
}
func encode(with coder: NSCoder) {
coder.encode(english, forKey: "english")
coder.encode(chinese, forKey: "chinese")
}
}
class List: NSObject, NSCoding {
let name: String
fileprivate var data = Data()
var answers: [Answer] {
get {
(try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)) as? [Answer] ?? []
}
set {
data = (try? NSKeyedArchiver.archivedData(withRootObject: newValue, requiringSecureCoding: false)) ?? Data()
}
}
init(name: String) {
self.name = name
}
required init(coder decoder: NSCoder) {
self.data = decoder.decodeData(forKey: "answersData")
self.name = decoder.decodeString(forKey: "name")
}
func encode(with coder: NSCoder) {
coder.encode(data, forKey: "answersData")
coder.encode(name, forKey: "name")
}
}
extension NSCoder {
func decodeString(forKey key: String) -> String {
return decodeObject(forKey: key) as? String ?? ""
}
func decodeData(forKey key: String) -> Data {
return decodeObject(forKey: key) as? Data ?? Data()
}
}
If you want to save your custom object in NSUserDefaults, it's not enough to make your class NSCoding-compliant -- you have to actually encode the data into an NSData object. This is a common mistake -- see my answer to another question for a similar situation.
So, you've added NSCoding to your Answer and List classes. That's a good start. Before you continue, you should verify that you've got that step right by using a NSKeyedArchiver to encode an example of a List object containing a few Answer objects into an instance of NSData, and then use NSKeyedUnarchiver to decode that data object back into your List. Verify that everything that you care about completes the round trip with no problems. This would be an excellent place to use Xcode's testing facility -- you could write a unit test that does exactly what I've described.
Once you know you've got the NSCoding stuff right, you should modify your code so that it encodes your List as NSData and stores the resulting data object in NSUserDefaults using the -setObject:forKey: method.

SWIFT How to create a NSCoding Subclass and call it from another class?

I found this black of code on NSCoding and it almost does want I want it to. the link for where I found it is below.
How do I create a NSCoding class and user in in other classes? The below code dies not work. I hope some can help me with this.
import Foundation
import UIKit
class User: NSObject, NSCoding {
var name: String
init(name: String) {
self.name = name
}
required init(coder aDecoder: NSCoder) {
self.name = aDecoder.decodeObjectForKey("name") as String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "name")
}
}
//new class where I want to set and get the object
class MyNewClass: UIViewController {
let user = User(name: "Mike")
let encodedUser = NSKeyedArchiver.archivedDataWithRootObject(user)
let decodedUser = NSKeyedUnarchiver.unarchiveObjectWithData(encodedUser) as User
}
//http://stackoverflow.com/questions/24589933/nskeyedunarchiver-fails-to-decode-a-custom-object-in-swift
I'm cutting and pasting from my own project below. I have limited this to one string parameter to store to file. But you can more of different types. You can paste this into a single swift file and use it as the ViewController plus added classes to test. It demonstrates using NSCoding with swift syntax to save and retrieve data in an object.
import UIKit
import Foundation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var instanceData = Data()
instanceData.name = "testName"
ArchiveData().saveData(nameData: instanceData)
let retrievedData = ArchiveData().retrieveData() as Data
println(retrievedData.name)
}
}
class Data: NSObject {
var name: String = ""
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(name, forKey: "name")
}
init(coder aDecoder: NSCoder!) {
name = aDecoder.decodeObjectForKey("name") as String
}
override init() {
}
}
class ArchiveData:NSObject {
var documentDirectories:NSArray = []
var documentDirectory:String = ""
var path:String = ""
func saveData(#nameData: Data) {
documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
documentDirectory = documentDirectories.objectAtIndex(0) as String
path = documentDirectory.stringByAppendingPathComponent("data.archive")
if NSKeyedArchiver.archiveRootObject(nameData, toFile: path) {
//println("Success writing to file!")
} else {
println("Unable to write to file!")
}
}
func retrieveData() -> NSObject {
var dataToRetrieve = Data()
documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
documentDirectory = documentDirectories.objectAtIndex(0) as String
path = documentDirectory.stringByAppendingPathComponent("data.archive")
if let dataToRetrieve2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? Data {
dataToRetrieve = dataToRetrieve2 as Data
}
return(dataToRetrieve)
}
}

Resources