swift - NSKeyedUnarchiver.unarchiveObject() 取消归档旧对象

标签 swift swift3 nsfilemanager nskeyedarchiver nskeyedunarchiver

我想将用户的过滤器选择保存在 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.getFilterSectionsFilterViewModel init 中设置 filterSections。我认为 self.getFilterSections 是一种返回默认值的方法。对我来说,情况不应该是这样,如果您已经归档了一些值,您应该在这个方法中得到它。

虽然,这本身不应该是问题的原因,但可能是诱发bug的原因。如果可能的话,尝试更改 self.getFilterSections 来返回存档值,否则返回默认值,并检查错误是否仍然存在。

关于swift - NSKeyedUnarchiver.unarchiveObject() 取消归档旧对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48285461/

相关文章:

ios - 在 UITableViewCell 中以编程方式添加标签?

ios - 类型 'CGPoint' 的值在 swift 3 中没有成员 'makeWithDictionaryRepresentation'

ios - 无法转换类型的值(NSSingleEntryDictionary)

swift - 在swift中判断一个目录是一个包

ios - 如何删除文件大小小于特定大小(字节)的文件夹内的所有文件

ios - 如何使用json解析选择所有 TableView 行并获取所有行的id?

ios - Swift - 对从核心数据中检索到的数组进行排序

Swift - View Controller 之间向前和向后的动画

ios - 传递依赖项,包括使用Google Map和集群的静态二进制文件

ios - 使用 [NSFileManager URLForUbiquityContainerIdentifier :] and [NSFileManager ubiquityIdentityToken]? 之间的主要区别是什么