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

  • Home
  • SEARCH
  • 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 6146585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:58:15+00:00 2026-05-23T18:58:15+00:00

I want to write a dictionary that has custom objects all of the same

  • 0

I want to write a dictionary that has custom objects all of the same type into the cache directory. I don’t know what is wrong but nothing is written. writeToFile returns false. This was working when I needed to save just a single user instance but now I need a dictionary. The dictionary keys are strings. The strange thing is initWithCoder and encodeWithCoder aren’t called at all.

I get the cache folder like this

NSString * NSCacheDirectory()
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
  NSString *cacheDirectory = [paths objectAtIndex:0]; 
#ifdef DEBUG
  NSString *regi = [NSHomeDirectory() stringByAppendingPathComponent: @"/Library/Caches/"];
#endif
  return  cacheDirectory;
  //return [NSHomeDirectory() stringByAppendingPathComponent: @"/Library/Caches/"];
}

I save a dictionary like this:

+(void) saveSharedUsers {
  NSString *cacheDir = NSCacheDirectory();
  NSFileManager *fm = [[NSFileManager alloc] init];
  Boolean success = true;
  NSError *error = nil;

  if (![fm fileExistsAtPath: cacheDir ])
    success = [fm createDirectoryAtPath: cacheDir withIntermediateDirectories: true attributes: nil error: &error];
  success = [[User sharedUsers] writeToFile: [NSCacheDirectory() stringByAppendingPathComponent: USER_FILE] atomically: true];
  [fm release];
}

User object:

@interface User : NSObject <NSCoding> {
  NSString    *email;
  NSInteger   userId;
  NSString    *name;
  NSString    *facebookId;
}

@property(nonatomic, copy)   NSString    *email;
@property(nonatomic, assign) NSInteger   userId;
@property(nonatomic, copy)   NSString    *name;
@property(nonatomic, copy)   NSString    *facebookId;
...
@end

@implementation User
...
@synthesize email,userId,name, facebookId;
- (void) encodeWithCoder:(NSCoder *)aCoder
{
  [aCoder encodeObject: self.name forKey:@"name"];
  [aCoder encodeObject: self.email forKey:@"email"];
  [aCoder encodeObject: self.facebookId forKey:@"facebookId"];
  [aCoder encodeInteger: self.userId forKey: @"userId"];
}

- (id) initWithCoder:(NSCoder *)aDecoder
{
  self = [super init];
  if (self != nil)
  {
    self.name = [aDecoder decodeObjectForKey: @"name"];
    self.email = [aDecoder decodeObjectForKey: @"email"];
    self.facebookId = [aDecoder decodeObjectForKey: @"facebookId"];
    self.userId = [aDecoder decodeIntegerForKey: @"userId"];
  }
  return self;
}
...
@end
  • 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-23T18:58:16+00:00Added an answer on May 23, 2026 at 6:58 pm

    I’m assuming that -[User sharedUsers] is an instance of NSDictionary. Then you’ll be writing a plist file to that location, not the archived data.

    You should try this:

    NSData *archivedUsers = [NSKeyedArchiver archivedDataWithRootObject:[User sharedUsers]];
    [archivedUsers writeToFile: [NSCacheDirectory() stringByAppendingPathComponent: USER_FILE] atomically: true];
    

    That will archive the objects and write the archived data to disk.

    Loading the data is the reverse operation:

    NSDictionary *users = [NSKeyedUnarchiver unarchiveObjectWithData:archivedUsers];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a homework assignment, just for all that want to know. I'm writing
I want to write a custom class that behaves like dict - so, I
I want to write some sort of dictionary that would accept current time and
I have a dictionary that I want to write to a csv file, but
I want to write in Delphi (2009 - so I have generic dictionary class)
I want to write a command that specifies the word under the cursor in
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an
I want to write a word addin that does some computations and updates some
I want to write some JavaScript that will change the onmousedown of a div

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.