Search code examples
iosxcodensuserdefaultsnskeyedarchiver

Where to store local variables? NSKeyedArchiver or NSUserDefaults?


bit of a noob here but what is the best way to save simple variables/arrays for an app? For example I have an app that has a friendList array and I want that to load each time the app starts? Would it be NSKeyedArchiver? NSUserDefualts? Or some other way? Another example is say I have a simple int variable postCount to track the number of posts made, this needs to be saved on exit and loaded. What would be the best approach? Thanks!


Solution

  • NSUserDefaults is best for small amount of data. If data is small the better option is NSUserDefaults else use NSKeyedArchiver.

    In your case friendList array can contain huge amount of data, so I prefer NSKeyedArchiver for that. For Storing the postCount you can use NSUserDefaults.