Welcome, Guest. Please login or register.
Did you miss your activation email?
July 27, 2016, 02:42:13 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 19 - Archiving
| | | |-+  Program 19.4 doesn't work
Pages: [1]   Go Down
Author Topic: Program 19.4 doesn't work  (Read 1553 times)
Posts: 17

« on: August 06, 2011, 05:52:30 AM »

I remade the program 19.4 a bit since I couldn't find the files it created. However it comes up with a SIGABRT error message. Can someone help explain why? The debugging says the error lies in line 29.

Code: (Objective-C)
// A program that creates a dictionary instance archives it to a file 
// then unarchives the file contents into a new dictionary instance

#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSStream.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSArchiver.h>

int main (int argc, const char * argv[])

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSDictionary *dictA = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"Storing an object for later use", @"archiving",
                           @"To implement all the methods defined in a protocol", @"adopt",
                           @"A class created to so that other classes can inherit from it", @"Abstract class",
    NSDictionary *dictB;
    if ([NSArchiver archiveRootObject: dictA toFile: @"dictA.archive"] == NO) {
        NSLog(@"archiving failed");
    dictB = [NSKeyedUnarchiver unarchiveObjectWithFile: @"dictA.archive"];
    for (NSString *key in dictB) {
        NSLog(@"%@: %@", key, [dictB objectForKey: key]);

    [pool drain];
    return 0;

And here is the error message:

This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys000
sharedlibrary apply-load-rules all
[Switching to process 8579 thread 0x0]
2011-08-06 14:51:34.300 Propertylists[8579:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: non-keyed archive cannot be decoded by NSKeyedUnarchiver'
*** First throw call stack:
   0   CoreFoundation                      0x00007fff8e555986 __exceptionPreprocess + 198
   1   libobjc.A.dylib                     0x00007fff95be5d5e objc_exception_throw + 43
   2   CoreFoundation                      0x00007fff8e5557ba +[NSException raise:format:arguments:] + 106
   3   CoreFoundation                      0x00007fff8e555744 +[NSException raise:format:] + 116
   4   Foundation                          0x00007fff9117185b -[NSKeyedUnarchiver initForReadingWithData:] + 631
   5   Foundation                          0x00007fff911a103a +[NSKeyedUnarchiver unarchiveObjectWithFile:] + 129
   6   Propertylists                       0x0000000100000b61 main + 433
   7   Propertylists                       0x00000001000009a4 start + 52
terminate called throwing an exception(gdb)
Global Moderator
Full Member
Posts: 114

« Reply #1 on: August 06, 2011, 10:18:20 AM »

Its saying that you have not got a keyed archive so the command you give cannot work.

Search in the mac os sdk manual in xcode for more information on how it works.

Note how you use archive to archive but keyed unarchive to unarchive.

the following is what it says for nsunarchiver in the sdk documentation

NSUnarchiver, a concrete subclass of NSCoder, defines methods for decoding a set of Objective-C objects from an archive. Such archives are produced by objects of the NSArchiver class.

In Mac OS X v10.2 and later, NSArchiver and NSUnarchiver have been replaced by NSKeyedArchiver and NSKeyedUnarchiver respectively—see Archives and Serializations Programming Guide.

So maybe you need to change line 25 to include NSKeyedArchiver instead and check out what happens then.
Posts: 17

« Reply #2 on: August 06, 2011, 10:55:23 AM »

Yes that solved the problem thx!
Global Moderator
Full Member
Posts: 114

« Reply #3 on: August 06, 2011, 10:59:49 AM »

No problem.
Pages: [1]   Go Up
Jump to:  

Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.