2017-09-26 89 views
0

我很接近用斯威夫特4和iOS 11.如何使用NSKeyedArchiver保存數據?

的應用程序有一個表視圖控制器和一個UITextView的對象,它是可編輯的詳細視圖中顯示的列表完成我的第一個iOS應用。我的目標是讓用戶能夠編輯UITextView中的內容並使用NSKeyedArchiver保存這些更改。

我有完整的列表視圖和詳細視圖連接。您可以進行編輯,但不能保存。

我已確認該條目確實保存到超出會話保留的內存,但編輯不保存。

評論文檔和通過多個教程工作尚未提供所需的見解。我重視的屏幕截圖顯示詳細信息視圖的界面,這裏是從詳細信息視圖控制器,其中保存按鈕觸發保存動作的代碼:

import UIKit 
import os.log 

class ViewController: UIViewController, UINavigationControllerDelegate, UITextViewDelegate { 

    var season: Season? 

    //MARK: Properties 

    @IBOutlet weak var seasonDetail: UITextView! 
    @IBAction func saveButton(_ sender: UIBarButtonItem) { 
    if let selectedDetail = seasonDetail.text { 
     seasonDetail.text = selectedDetail 
    } else { 
     print("failed to save changes.") 
    } 
    saveChanges() 
    print("Save button clicked") 
    } 

    override func viewDidLoad() { 
    super.viewDidLoad() 

     title = season?.name 
     seasonDetail.text = season?.detail 
    seasonDetail.delegate=self 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
    season?.detail = (seasonDetail?.text)! 
    } 

    func textViewDidEndEditing(_ textView: UITextView) { 
    seasonDetail.text = season?.detail 
    } 

    //MARK: UITextViewdDelegate 

    func textViewShouldReturn(_ textView: UITextView) -> Bool { 
    textView.resignFirstResponder() 

    return true 
    } 


func saveChanges() { 
    print("Saving items to: \(Season.ArchiveURL)") 
    let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(season as Any, toFile: Season.ArchiveURL.path) 

    if isSuccessfulSave { 
     os_log("Season sucessfully saved.", log: OSLog.default, type: .debug) 
    } else { 
     os_log("Failed to save season.", log: OSLog.default, type: .debug) 
    } 
    } 

} 

這是從數據模型類的代碼:

import UIKit 
import os.log 

class Season: NSObject, NSCoding { 

    //MARK: Properties 

    var name: String 
    var detail: String 

    //MARK: Archiving Paths 

    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first! 
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("season") 

    //MARK: Types 

    struct PropertyKey { 
    static let name = "name" 
    static let detail = "detail" 
    } 


    //MARK: Initialization 
    init?(name: String, detail: String) { 

    guard !name.isEmpty else { 
     return nil 
    } 

    guard !detail.isEmpty else { 
     return nil 
    } 

    // Initialize stored properties 
    self.name = name 
    self.detail = detail 

    } 

    //MARK: NSCoding 

    func encode(with aCoder: NSCoder) { 
    aCoder.encode(name, forKey: PropertyKey.name) 
    aCoder.encode(detail, forKey: PropertyKey.detail) 
    } 

    required convenience init?(coder aDecoder: NSCoder) { 

    // the name is required. If we cannnot get a name string, the initializer should fail. 
    guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String 
     else { 
     os_log("Unable to decode the name for a Season object.", log: OSLog.default, type: .debug) 
     return nil 
    } 

    let detail = aDecoder.decodeObject(forKey: PropertyKey.detail) 

    self.init(name: name, detail: detail as! String) 
    } 
    } 

我的目標是要了解什麼是我的代碼失蹤,知道如何保持所有數據,包括編輯。我將不勝感激任何方向。

enter image description here

回答

0

請檢查:

class ViewController: UIViewController, UINavigationControllerDelegate, UITextViewDelegate { 
    var season: Season? 
    @IBOutlet weak var seasonDetail: UITextView! 
    @IBAction func saveButton(_ sender: UIBarButtonItem) { 
     if let selectedDetail = seasonDetail.text { 
      season?.detail = selectedDetail // this is the line 
     } else { 
      print("failed to save changes.") 
     } 
     saveChanges() 
     print("Save button clicked") 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     if season == nil { 
      season = Season(name: "Season Name", detail: "Season Details") 
     } 
     title = season?.name 
     seasonDetail.text = season?.detail 
     seasonDetail.delegate=self 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     season?.detail = (seasonDetail?.text)! 
    } 

    func textViewDidEndEditing(_ textView: UITextView) { 
     season?.detail = seasonDetail.text 
    } 

    //MARK: UITextViewdDelegate 
    func textViewShouldReturn(_ textView: UITextView) -> Bool { 
     textView.resignFirstResponder() 
     return true 
    } 

    func saveChanges() { 
     print("Saving items to: \(Season.ArchiveURL)") 
     let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(season as Any, toFile: Season.ArchiveURL.path) 

     if isSuccessfulSave { 
      os_log("Season sucessfully saved.", log: OSLog.default, type: .debug) 
     } else { 
      os_log("Failed to save season.", log: OSLog.default, type: .debug) 
     } 
    } 
} 
+0

嗨VINI,我怎麼指示更新的價值?謝謝。 – fmz

+0

我通過'seasonDetail.text'更新了詳細信息。 –

+0

這工作? –

相關問題