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

The Archive Base Latest Questions

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

I am attempting to use the Keyed Archiver classes for the first time and

  • 0

I am attempting to use the Keyed Archiver classes for the first time and I’m failing the last assert in this simple test (OCUnit):

- (void) testNSCoding
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
    [dict setObject:@"hello" forKey:@"testKey"];

    NSMutableData* data = [NSMutableData data];
    NSKeyedArchiver *ba = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [ba encodeRootObject:dict];
    [ba finishEncoding];

    STAssertTrue(data.length != 0, @"Archiver gave us nothing.");

    NSKeyedUnarchiver *bua = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    id decodedEntity = [bua decodeObjectForKey:@"root"];
    [bua finishDecoding];
    STAssertNotNil(decodedEntity, @"Unarchiver gave us nothing.");
}

I have confirmed that the archiver is archiving, I’m assuming the issue exists in the unarchiving.
According to the Archives and Serializations Guide I believe that perhaps there is some issue with how I am using the Unarchiver?

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-21T21:41:14+00:00Added an answer on May 21, 2026 at 9:41 pm

    First, you should not use the encodeRootObject method. That’s a legacy method defined in NSCoder to support obsolete non-keyed archivers, and can only be decoded using decodeObject:. You only use the pair of encodeObjectForKey: and decodeObjectForKey:.

    So,

     id decodedEntity = [bua decodeObjectForKey:@"root"];
    

    should be

     id decodedEntity = [bua decodeObjectForKey:@"testKey"];
    

    If you want to decode the totality of a dictionary, instead of

    [ba encodeRootObject:dict];
    

    do

    [ba encodeObject:dict forKey:@"root"];
    

    By the way, for simple purposes it often suffices to use NSUserDefaults, which automatically takes care of creating the file to write on, writing things on the file, and reading it when the program is launched the next time.

    If you just need to encode a dictionary, using NSPropertyListSerialization usually suffices.

    If you do use NSKeyedArchiver and NSKeyedUnarchiver, I recommend you to follow the practice and write encodeWithCoder: and initWithCoder: for an object.

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

Sidebar

Related Questions

We are attempting to use a SQL Server 2003 database for our test records
Attempting to use the data series from this example no longer passes the JSONLint
Attempting to use the amazon API to obtain product data and currently failing miserably.
I am attempting to use this code sample to control the Windows XP On-Screen
I am attempting to use this method I've constructed to save the layer of
In attempting to use the Performance Tools on an ASP.NET website I'm getting various
I'm attempting to use Mono to load a bitmap and print it on Linux
I'm attempting to use TinyXML to read and save from memory, instead of only
I am attempting to use the 'tempfile' module for manipulating and creating text files.
I'm attempting to use extension methods to add an operater overload to the C#

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.