2017-09-04 69 views
0

好日子,NSKeyedUnarchiver不返回「無」

我在整理從代碼學院教程應用的邊緣,我真的與它的歸檔玩弄和數據「解除存檔並與文件」。所以基本上歸檔工作,這是下面的代碼。

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

因此,正確地創建文件,然後保存類型訂單的數據。

但是,當我嘗試使用下面的實現檢索數據時,似乎返回的數據標記爲「」。

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

我要打印的「數」的原因是爲了能夠確保未包裝的對象有值,但代碼不去那裏意味着對象是

存儲變量如下之前我做的視圖控制器因此,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); 

    } 


} 

希望你能說點什麼。

回答

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

我編輯了帖子,並顯示了兩個類。感謝您查看這個。 – josephnicholas

+0

使用編碼(_:forKey :)方法編碼您的屬性。 –

+0

在哪一類?我已經放置它。 – josephnicholas