2016-07-29 7 views
1

NSCodingに準拠したオブジェクトです。私は保存し、スウィフト3のアプリケーションのドキュメントディレクトリから回復したいと思います。私はそれが保存方法と回復方法であると想像します。これはどうですか?持続性を使用し、Swift 3のアプリケーションディレクトリにNSCoding準拠のオブジェクトを取得する方法は?

import Foundation 

class Book: NSObject, NSCoding { 
    var title: String 
    var author: String 
    var pageCount: Int 
    var categories: [String] 
    var available: Bool 

    init(title:String, author: String, pageCount:Int, categories:[String],available:Bool) { 
     self.title = title 
     self.author = author 
     self.pageCount = pageCount 
     self.categories = categories 
     self.available = available 
    } 

    // MARK: NSCoding 
    required convenience init?(coder: NSCoder) { 

     let title = coder.decodeObject(forKey: "title") as! String 
     let author = coder.decodeObject(forKey: "author")as! String 
     let categories = coder.decodeObject(forKey: "categories") as! [String] 
     let available = coder.decodeBool(forKey: "available") 
     let pageCount = coder.decodeInteger(forKey: "pageCount") 

     self.init(title:title, author:author,pageCount:pageCount,categories: categories,available:available) 
    } 

    func encode(with: NSCoder) { 
     with.encode(self.title, forKey: "title") 
     with.encode(self.author, forKey: "author") 
     with.encode(Int32(self.pageCount), forKey: "pageCount") 
     with.encode(self.categories, forKey: "categories") 
     with.encode(self.available, forKey: "available") 
    } 
} 

ありがとうございます!

+0

'NSKeyedArchiver' /' NSKeyedUnarchiver' – dan

+0

私はあなたがNSKeyedArchiver/NSKeyedUnarchiverを使用することを理解するが、私は、docsディレクトリとそれを保存するためのコードへの参照を取得する方法を理解していません。私は午後すべてドキュメントとウェブの周りをバウンスしてきました。 – Aaronium112

答えて

1

省:

// Get documents directory 
if let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first { 

    // Append your file name to the directory path 
    let path = (docs as NSString).appendingPathComponent("filename") 

    // Archive your object to a file at that path 
    NSKeyedArchiver.archiveRootObject(yourObject, toFile: path) 
} 

ロード:

// Get documents directory 
if let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first { 

    // Append your file name to the directory path 
    let path = (docs as NSString).appendingPathComponent("filename") 

    // Unarchive your object from the file 
    let yourObject = NSKeyedUnarchiver.unarchiveObject(withFile: path) as? Book 

    // do whatever with yourObject 
} 
1

ここでガード文を使用して、同じデザインです。

import UIKit 
import MediaPlayer 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let book = Book(title: "Atlas Shrugged", author: "Ayn Rand", pageCount: 10, categories: ["Ethics"], available: true) 
     save(object: book, filename: "Bookshelf") 

     let atlasShrugged = retrieve(filename: "Bookshelf") as! Book 
     print("\(atlasShrugged.title) by \(atlasShrugged.author)") 
    } 

    func save(object:NSObject ,filename:String){ 

     //build filepath in doc directory 
     guard let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return } 
     let path = (docs as NSString).appendingPathComponent(filename) 

     //save object to path in docs 
     NSKeyedArchiver.archiveRootObject(object, toFile: path) 
    } 

    func retrieve(filename:String) -> AnyObject? { 

     // Get documents directory 
     guard let docs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { return nil } 
     //build path to file 
     let path = (docs as NSString).appendingPathComponent(filename) 
     //Unarchive from doc directory 
     let object = NSKeyedUnarchiver.unarchiveObject(withFile: path) as? Book 
     return object 
    } 
関連する問題