NSKeyedUnarchiver 不返回 "nil"

NSKeyedUnarchiver not returning the "nil"

美好的一天,

我即将完成 Code School 的教程应用程序,我真的在玩它的归档和取消归档数据 "to and with a file"。所以本质上是归档工作,也就是下面的代码。

class func saveOrdersToArchive(cart: Orders) -> Bool {
    print(archiveFilePath());
    return NSKeyedArchiver.archiveRootObject(cart, toFile: archiveFilePath());
}

archiveFilePath() 函数是这样实现的,它基本上创建了一个名为 "cart.archive" 的文件并将其存储在模拟器的本地驱动器上。

class func archiveFilePath() -> String {
    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0];
    return documentsDirectory.appendingPathComponent("cart.archive").path;
}

所以它正确地创建了文件,然后存储了 Orders.

类型的数据

但是当我尝试使用下面的实现检索数据时,返回的数据似乎被标记为“nil”。

class func readOrdersFromArchive() -> Orders? {
    print(archiveFilePath());
    return NSKeyedUnarchiver.unarchiveObject(withFile: archiveFilePath()) as? Orders
}

所以,在主ViewController文件中,下面实现了Object的保存。

//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));

该函数打印为真,表示存档成功。

检索数据的实现如下,

if let order1 = Orders.readOrdersFromArchive(){
    order = order1
    if let o = order.orders{
        if let n = o.names{
            print(n.count)
        }
    }
}

我想打印 "count" 的原因是为了能够确保展开的对象有值,但代码不会去那里意味着对象是 nil.

我在 ViewController 中进行初始化,然后按如下方式存储变量,

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]()));

显示订单 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());
    }

显示产品 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);

    }


}

希望你能有所启发。

向我们展示您的订单 class。它是否符合 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")
    }
}