NSKeyedUnarchiver.unarchiveObject() 取消归档旧对象
NSKeyedUnarchiver.unarchiveObject() unarchives old object
我想保存用户在 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)
}
您正在使用 self.getFilterSections
在 FilterViewModel init
中设置 filterSections
。我想 self.getFilterSections
是一种 return 是默认值的方法。对我来说,情况并非如此,如果你已经归档了一些值,你应该在这个方法中得到它。
虽然,这不应该是问题的原因,但是可能是诱发bug的原因。如果可能,尝试将 self.getFilterSections
更改为 return 存档值,否则使用默认值并检查错误是否仍然存在。
我想保存用户在 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)
}
您正在使用 self.getFilterSections
在 FilterViewModel init
中设置 filterSections
。我想 self.getFilterSections
是一种 return 是默认值的方法。对我来说,情况并非如此,如果你已经归档了一些值,你应该在这个方法中得到它。
虽然,这不应该是问题的原因,但是可能是诱发bug的原因。如果可能,尝试将 self.getFilterSections
更改为 return 存档值,否则使用默认值并检查错误是否仍然存在。