【问题标题】:NSKeyedUnarchiver.unarchiveObject() unarchives old objectNSKeyedUnarchiver.unarchiveObject() 取消归档旧对象
【发布时间】:2018-01-16 16:10:24
【问题描述】:

我想将用户的过滤器选择保存在FilterViewController

FilterViewController 关闭时,NSKeyedArchiver.archiveRootObject 归档用户的选择。但是,NSKeyedUnarchiver.unarchiveObject 会打开初始默认选择(不是用户的选择)。 如何解决这个问题?

FiltersViewController.swift

override func viewWillAppear(_ animated: Bool) {
       if let filterSections = NSKeyedUnarchiver.unarchiveObject(withFile: filterViewModel.filtersFilePath) as? [FilterSection] {
            // Retrieves initial default selections, NOT user's selection
            filterViewModel.filterSections = filterSections 
            filtersTableView.reloadData()
        }
    }

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // Saves what user selects
    let isSuccessful = NSKeyedArchiver.archiveRootObject(self.filterViewModel.filterSections, toFile: self.filterViewModel.filtersFilePath) 
    if (isSuccessful) {
        print("Saved filters") // This is printed
    } else  {
        print("Didn't Save filters")
    }
}

FilterViewModel.swift

class FilterViewModel: NSObject {
    // Contains all filtered sections displayed on table view
    var filterSections: [FilterSection] = []
    // File Path to saved Filter Sections
    var filtersFilePath: String {
        let manager = FileManager.default
        let url = manager.urls(for: .documentDirectory, in: .userDomainMask).first
        print("this is the url path in the documentDirectory \(url)")
        return (url!.appendingPathComponent("FilterSelectionData").path)
    }

    override init() {
         super.init()
         filterSections = self.getFilterSections()
    }

}

CompanyViewController.swift

@objc func filterButtonTapped() {
        var filterViewModel: FilterViewModel
        if (self.filterViewModel != nil) {
            filterViewModel = self.filterViewModel! // This runs
        }
        else {
            self.filterViewModel = FilterViewModel()
            filterViewModel = self.filterViewModel!
        }
        let filtersVC = FiltersViewController(filterViewModel: filterViewModel)
        self.navigationController?.pushViewController(filtersVC, animated: true)

    }

【问题讨论】:

  • 您是否也在某处归档默认值?
  • 您是否在FilterViewModel 中实现了NSCodingnshipster.com/nscoding
  • @Puneet Sharma 首次初始化视图模型时没有创建默认值
  • @MilanNosáľ 我认为我不需要? B/c 我正在归档 filterViewModel 的部分 (filterViewModel.filterSections)
  • @Alex 哦,是的,我的错误.. 所以我猜你是在 FilterSection.. 抱歉..

标签: swift swift3 nsfilemanager nskeyedarchiver nskeyedunarchiver


【解决方案1】:

您正在使用self.getFilterSectionsFilterViewModel init 中设置filterSections。我想self.getFilterSections 是一种返回默认值的方法。对我来说,不应该是这样,如果你已经归档了一些值,你应该在这个方法中得到它。

虽然,这本身不应该是问题的原因,但可能是一个原因用于诱导错误。如果可能,请尝试更改 self.getFilterSections 以返回存档值,否则为默认值并检查错误是否仍然存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    • 2016-03-03
    • 1970-01-01
    相关资源
    最近更新 更多