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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:38:20+00:00 2026-05-23T09:38:20+00:00

Previously, I have an array in which I use NSKeyedArchiver to archieve. [NSKeyedArchiver archiveRootObject:array

  • 0

Previously, I have an array in which I use NSKeyedArchiver to archieve.

[NSKeyedArchiver archiveRootObject:array toFile:docPath];

Called on applicationWillTerminate and applicationDidEnterBackground.

Upon starting up, in didFinishLaunchingWithOPtions, the array is unarchieved:

NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];

Everything was good. Since then, I added a Singleton class for Settings variables. I would like to archive the Singleton as well. How would I do that?

The part that confuses me is if I call the same message:

[NSKeyedArchiver archiveRootObject:singleton toFile:docPath];

How can the app know which object I want to unarchive when I call:

[NSKeyedUnarchiver unarchiveObjectWithFile:docPath];

Is it the array or the singleton?

The archiveRootObject accepts an (id) which to me means whatever I want. I can create a data class which includes the array and the singleton. Is that how I am suppose to do this? Is there a better way? Any suggestions are welcomed.

Thanks!

  • 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-23T09:38:21+00:00Added an answer on May 23, 2026 at 9:38 am

    If you want to encode more than one object at the root level, you’ll need to create the archiver using +alloc and -initForWritingWithMutableData: yourself, send it -encodeObject:forKey: messages for each of the objects you want to add to the archive, and finally -finishEncoding. You can then write the data to a file yourself.

    It’ll look something like this (warning: untested code ahead):

    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    
    [archiver encodeObject:someObject forKey:@"people"];
    [archiver encodeObject:anotherObject forKey:@"places"];
    [archiver encodeObject:thirdObject forKey:@"things"];
    [archiver finishEncoding];
    
    [data writeToURL:someUrl atomically:YES];
    [archiver release];
    

    To retrieve the objects, you’ll do essentially the opposite: read a file into an NSData object; create an NSKeyedUnarchiver with +alloc and -initForReadingWithData:; retrieve the objects you care about using -decodeObjectForKey:; and finally call -finishDecoding. You can almost read the sample above from bottom to top with a few name changes.

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

Sidebar

Related Questions

I have previously been told that I should always use Randomize() before I use
I have previously asked a question to populate a page depending on which link
I am writing a C program which has to use a 2D array to
I have a situation where I have two arrays and I use one array(A)
I have an array of objects which will be the basis for a certain
So, I have observable array with sites, which is shown via template. If I'll
I previously have been reading NMEA data from a GPS via a serial port
Previously I have asked to strip text from a field and convert it to
I have previously written user interfaces using .NET and Windows Forms. I'm about to
I have previously read Spolsky's article on character-encoding, as well as this from dive

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.