Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4241020
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:14:48+00:00 2026-05-21T03:14:48+00:00

I need to save a NSMutableDictionary in NSUserDefaults. I have searched the web for

  • 0

I need to save a NSMutableDictionary in NSUserDefaults. I have searched the web for many examples, but have not received any ones that are relevant enough. Can somebody post a small piece of code which will save a NSMutableDictionary in NSUserDefaults at shutdown and retrieve it at launch of the app? Also could you please tell me where to place the code.

I have some code myself, but it is not working.

appDelegate.h – retrieval of data

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      NSData *dat = [defaults objectForKey:@"theKey"];
      tmpArray  = [[NSMutableArray alloc] init];
      tmpArray = [NSKeyedUnarchiver unarchiveObjectWithData:dat];

      [self.window makeKeyAndVisible];
      [window addSubview:tabBarController.view];
      return YES;
}

appDelegate.h – storage of Data

- (void)applicationWillTerminate:(UIApplication *)application {

      [self saveContext];
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
      NSData *dat = [NSKeyedArchiver archivedDataWithRootObject:tmpArray];
      [defaults setObject:dat forKey:@"theKey"];
}

Yes, it is for an array, but I will need one for a NSMutableDictionary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:14:49+00:00Added an answer on May 21, 2026 at 3:14 am

    The discussion of applicationWillTerminate:

    For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason.

    To save your object you should put your code into applicationDidEnterBackground: too.

    In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution. You should use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. You should also disable updates to your application’s user interface and avoid using some types of shared system resources (such as the user’s contacts database). It is also imperative that you avoid using OpenGL ES in the background.


    To save a dictionary instead of an array simply replace all occurrences of Array with Dictionary


    And fix the leak

    tmpArray  = [[NSMutableArray alloc] init]; // not used, not released but `alloc`ed.
    tmpArray = [NSKeyedUnarchiver unarchiveObjectWithData:dat];
    

    by removing the first line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used NSUserDefaults to save NSMutableDictionary. But when I am retrieving the value from
I need you save my live. I have a table with a LOT of
I need to save an object, its serializable but I donot want to use
I need to save and load properties of a Class dynamicly, but what is
I need to save client machine time when he log in on my web
I need to save on shared preferences some array of Strings and after that
I need to save selected items for my UITableView in the NSMutableDictionary for iplementing
I need to save some link in mysql, but some link are smaller and
I need to save my InfoPath form regardless of whether the required fields have
I need to save the video into a sequence of images while recording that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.