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 4126846
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:05:10+00:00 2026-05-21T00:05:10+00:00

I would like to have a permanent NSDictionary in my app, in which from

  • 0

I would like to have a “permanent” NSDictionary in my app, in which from the beginning of the app launch, I can get access to the elements and even after I kill the app and start it again.
This NSDictionary needs to be stored tightly with the app. One way would be to just to create the NSDictionary from a Web Service data every time the app launches, then create a singleton class that would represent this NSDictionary, but I don’t think this is good.

The NSDictionary will approximately hold 10-20 objects, where the key would be a NSString or NSDate and the value would be a NSArray. The NSArray would have a maximum of approximately 50 entries in it (on average probably there will only be 5-25 entries).

I am planning to use this NSDictionary as a part of a calculation that I am doing inside the delegate locationManager:didUpdateToLocation:fromLocation: each time a user moves every 50-100 meters.

Suggestions are appreciated on the best way I could do this.

  • 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-21T00:05:11+00:00Added an answer on May 21, 2026 at 12:05 am

    Just having the NSDictionary in a singleton will not keep it between app launches, you’ll need to save it to disk, and then read it from the disk when the app starts.

    If you have no custom objects (subclasses you’ve created) in the NSDictionary, or in the NSArrays or non at all, you can use this method to save the NSDictionary is:

    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

    and to open the dictionary from disk:

    - (id)initWithContentsOfFile:(NSString *)path


    However if you do have custom objects they will need to conform to the NSCoding protocol. And you’ll have to use 2 different methods to save and open it:

    NSCoding is a protocol, so in your header you need to add it on the end of the interface line:

    @interface myClassName : NSObject <NSCoding> {
    

    (where the only thing you should add is <NSCoding>)

    Then in your implementation of your subclass, you need to add the following methods:

    - (id)initWithCoder:(NSCoder *)decoder

    and:

    - (void)encodeWithCoder:(NSCoder *)encoder

    The initWithCoder: method gets called when you want to unarchive(/open) your NSDictionary (which somewhere contains this class)

    encodeWithCoder: is what gets called when your NSDictionary is archived(/saved).
    You don’t call either of these yourself. You need to add code in them:

    - (id)initWithCoder:(NSCoder *)decoder {
        if ((self = [super initWithCoder:decoder])) {
            aProperty = [[decoder decodeObjectForKey:@"aProperty"] retain];
            anotherProperty = [[decoder decodeObjectForKey:@"anotherProperty"] retain];
            aFloat = [decoder decodeFloatForKey:@"aFloat"];
        }
        return self;
    }
    
    - (void)encodeWithCoder:(NSCoder *)encoder {
        [super encodeWithCoder:encoder];
        [encoder encodeObject:aProperty forKey:@"aProperty"];
        [encoder encodeObject:anotherProperty forKey:@"anotherProperty"];
        [encoder encodeFloat:aFloat forKey:@"aFloat"];
    }
    

    You need to have similar lines for each value you want to store (generally all the properties, [and instance variables] your class has). Note the how the float line is different to the others.

    The keys can be any string you want, as long each property has it’s own unique key and that they match between the two methods. I personally use the name of the property as it’s just easier to understand.

    when you actually want to save your NSDictionary you use:

    [NSKeyedArchiver archiveRootObject:myDictionary toFile:pathToMyDictionary];
    

    and to open the dictionary:

    NSDictionary *myDictionary = [NSKeyedUnarchiver unarchiveObjectWithFile:pathToMyDictionary];
    

    (depending on your code you may need to retain myDictionary)


    To get the path to your dictionary (for both saving and opening) do:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pathToMyDictionary = [documentsDirectory stringByAppendingPathComponent:@"myDictionary.dat"];
    

    Hope that helps, if you have any more questions about this answer, just comment 🙂

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

Sidebar

Related Questions

I would like to have a field which is updated on every change(insertion, modification),
I would like to have a regular expression which clears an HTML field when
I would like to have a makefile copy files from another directory and change
I have an ActiveRecord object and I would like to prevent it from being
I have my custom theme, in which I would like to make changes to
I have and URL which is homepage/profile.php?id=15&username=dinesh.kumar now i would like to generate URL
So in my project i would like have a nice treeview that has images.
I would like to have an AppWidget that designed like this one . Image:
I would like to have a more colorful Python prompt in the terminal, just
I would like to have the cursor in the JavaDoc area when creating interfaces,

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.