ios - 将数组保存到 userDefault

标签 ios objective-c arrays swift3 nsuserdefaults

我的要求是用户必须导航上次访问的页面,因此

我试图将UIViewController数组保存到NSUserDefault,但应用程序每次都会崩溃

let array = self.navigationController?.viewControllers
UserDefaults.standard.set(array, forKey: "LastNavigationArray")

对于这次尝试崩溃是

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (

另外,我尝试过以下代码,但没有成功

let array = self.navigationController?.viewControllers
let encodedData = NSKeyedArchiver.archivedData(withRootObject: array)

UserDefaults.standard.set(encodedData, forKey: "LastNavigationArray") 

对于这次崩溃是

Assertion failure in -[UICGColor encodeWithCoder:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UIColor.m:1549
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only RGBA or White color spaces are supported in this situation.'

如果这是错误的,请建议我正确的实现方法。 因为我的导航流程非常复杂。

最佳答案

UIViewController 是保存在 UserDefaults 中的非常重的对象。

您可以为自己保留一个自定义对象来标识导航堆栈中的所有 View Controller 。例如,您可以使用每个单独的 View Controller 的类名字符串构造自定义字符串。或者,更好的选择是仅将最后一个 View Controller 的类名存储在用户默认值中。当用户再次打开您的应用程序时,您可以通过检查类名称来实例化并呈现该 View Controller 。

关于ios - 将数组保存到 userDefault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45439037/

相关文章:

java - 如何编写一个方法来计算数组中所有项目的总成本?

python - 如何使用 numpy 对循环数组进行切片

ios - iOS 设备是否允许未对齐的内存访问?

iphone - 在已部署的应用程序上推送一个 nil View Controller

ios - MobileVlcKit 编译失败

ios - calayer 没有根据 tableview 单元格中的 UIView 大小而改变

objective-c - NSNotificationCenter 作用域定义

javascript - 如何从两个数组创建一个数组

iphone - 想从邮政编码中获取州名

ios - 在 iOS 中以编程方式获取应用程序的版本