0

I am trying to use NSCoder to archive a custom object (my object is a library, as in a place to check out books) to the iPhone's file system. However, I am getting an error "unrecognized selector sent to instance" when I attempt to call NSKeyedArchiver archiveRootObject. The library declares the NSCoding protocol and implements the required methods. I'm at a loss.

The object Interface (library.h):

#import <Foundation/Foundation.h>
@interface Library : NSObject <NSCoding> {
int numCopies;
NSString *location;
NSString *locationInfo;
NSString *material; 
NSString *library;
}
+ (id)library;
@property (nonatomic) int numCopies;
@property (retain) NSString *location;
@property (retain) NSString *locationInfo;
@property (retain) NSString *material;
@property (retain) NSString *library;
- (void)encodeWithCoder:(NSCoder *)encoder;
- (void)initWithCoder:(NSCoder *)decoder;
@end

The relevant part of object implementation (library.m):

#import "library.h"

@implementation Library

@synthesize numCopies;
@synthesize location;
@synthesize locationInfo;
@synthesize material;
@synthesize library;

+ (id)library
{
return [[Library alloc] init];  
}

-(void)encodeWithCoder:(NSCoder *)encoder
{
    //for each instance variable, archive it under its variable name
    [encoder encodeObject:numCopies forkey:@"numCopies"];
    [encoder encodeObject:location forkey:@"location"];
    [encoder encodeObject:locationInfo forkey:@"locationInfo"];
    [encoder encodeObject:material forkey:@"material"];
    [encoder encodeObject:library forkey:@"library"];
}

Finally, the viewController (libTableViewController.m)

//try to archive the library object
NSString *savePath = pathInDocumentDirectory(@"lib.data");
NSLog(@"try to archive library to path: %@", savePath);
[NSKeyedArchiver archiveRootObject:this_lib toFile:savePath];

Here is the error:

2011-12-18 12:13:38.799 LibraryFiend2[12877:207] try to archive library to path: /Users/kclary/Library/Application Support/iPhone Simulator/4.3/Applications/71B11434-3307-449A-9E95-5E22034E7FA0/Documents/lib.data
2011-12-18 12:13:38.800 LibraryFiend2[12877:207] -[NSKeyedArchiver encodeObject:forkey:]: unrecognized selector sent to instance 0x4b1a130
2011-12-18 12:13:38.801 LibraryFiend2[12877:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSKeyedArchiver encodeObject:forkey:]: unrecognized selector sent to instance 0x4b1a130'
terminate called after throwing an instance of 'NSException'  

If I substitute a simple array for my library object, it works.

1 Answer 1

2

You got exception because of this line [encoder encodeObject:numCopies forkey:@"numCopies"];

numCopies is not an object, it's a primitive and should be stored using encodeInt:forKey: method.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, that did not fix the problem. I changed the line to: [encoder encodeInt:numCopies forkey:@"numCopies"]; but I am getting the same exception on that line of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.