NSKeyedUnarchiver not returning the "nil"

322 views Asked by At

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.

1

There are 1 answers

4
Harry Netzer On BEST ANSWER

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")
    }
}