0

My Requirement is user has to navigate last visited page so

I am trying to save array of UIViewController to NSUserDefault but app is crashing every time

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

For this try crash is

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

Also, I have tried with following code but no luck

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

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

For this crash is

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.'

If this is wrong please suggest me the correct way to implement it. Because I have very complex navigation flow.

13
  • 2
    The crash has nothing to do with your saving of array as per logs Commented Aug 1, 2017 at 13:24
  • @kapsym NO it is crashing with same log Commented Aug 1, 2017 at 13:27
  • @kapsym Do you want me to put whole stack trace ? Commented Aug 1, 2017 at 13:29
  • I suppose that only RGBA or White color spaces are supported in this situation. It'd be so helpful, if Apple gave you hints in their crash logs... Commented Aug 1, 2017 at 13:29
  • @MikeAlter If you just want to save the VC where the user was last, can you just save the identifier. Any reason to save entire Viewcontrollers to user defaults? Commented Aug 1, 2017 at 13:31

1 Answer 1

1

UIViewControllers are really heavy objects to save in UserDefaults.

You can keep a custom object for yourself to identify all the view controllers in navigation stack. For example you can construct a custom string with each individual view controller's class name strings. Or, a better option would be only store last view controller's class name in user defaults. You can instantiate and present that view controller by checking the class name when user opens your app again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.