NSKeyedUnarchiver没有返回" nil"

时间:2017-09-04 19:55:29

标签: ios swift nskeyedarchiver nskeyedunarchiver

美好的一天,

我即将完成Code School的教程应用程序,我真正在玩它的归档和解压缩数据"以及文件"。基本上归档工作,这是下面的代码。

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;
}

因此,它会正确创建文件,然后存储订单类型的数据。

但是当我尝试使用下面的实现检索数据时,似乎返回的数据标记为" 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));

该功能打印为true,表示存档成功。

检索数据的实现在下面实现,

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

我想要打印"计数"是为了能够确保展开的对象具有值,但代码没有去那里意味着对象 nil

我在ViewController中执行init,然后按如下方式存储变量,

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

    }


}

希望你能解释一下。

1 个答案:

答案 0 :(得分:0)

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