NSUserDefaults cannot save/load NSData

Discussion in 'Mac Programming' started by Soulstorm, Dec 15, 2007.

  1. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I have 2 classes, one contain another. I am trying to save an Object of the ContainerClass using NSUserDefaults. Since those classes are not property lists, I wrote methods that conform to <NSCoding> and I try to save them using NSData. Here are the 3 classes.

    class1:
    Code:
    #import <Cocoa/Cocoa.h>
    
    
    @interface Class1 : NSObject<NSCoding> {
    	NSString *value;
    	NSString *key;
    }
    
    -(NSString *)value;
    -(NSString *)key;
    
    -(void)setValue:(NSString *)otherValue;
    -(void)setKey:(NSString *)otherKey;
    
    @end
    
    @implementation Class1
    
    -(NSString *)value
    {
    	return value;
    }
    -(NSString *)key
    {
    	return key;
    }
    
    -(void)setValue:(NSString *)otherValue
    {
    	if (value != nil)
    		[value release];
    	value = [otherValue retain];
    }
    
    -(void)setKey:(NSString *)otherKey
    {
    	if (key != nil)
    		[key release];
    	key = [otherKey retain];
    }
    
    
    
    -(id)initWithCoder:(NSCoder *)coder
    {
    	if (value != nil)
    		[value release];
    	if (key != nil)
    		[key release];
    	key = [coder decodeObjectForKey:@"key"];
    	value = [coder decodeObjectForKey:@"value"];
    	return self;
    }
    
    -(void)encodeWithCoder:(NSCoder *)coder
    {
    	[coder encodeObject:key forKey:@"key"];
    	[coder encodeObject:value forKey:@"value"];
    }
    
    
    
    - (void) dealloc
    {
    	[value release];
    	[key release];
    	[super dealloc];
    }
    @end
    
    
    ContainerClass:
    Code:
    #import <Cocoa/Cocoa.h>
    #import "Class1.h";
    
    @interface ContainerClass : NSObject<NSCoding> {
    	NSMutableArray *objects;
    }
    
    -(NSMutableArray *)objects;
    -(void)setObjects:(NSArray *)anArray;
    
    @end
    @implementation ContainerClass
    - (id) init
    {
    	self = [super init];
    	if (self != nil) {
    		Class1 *object1 = [[Class1 alloc]init];
    		[object1 setValue:@"hello1" forKey:@"key"];
    		[object1 setValue:@"world1" forKey:@"value"];
    		
    		objects = [[NSMutableArray alloc]init];
    		[objects addObject:object1];
    	}
    	return self;
    }
    
    
    
    -(NSMutableArray *)objects
    {
    	return objects;
    }
    -(void)setObjects:(NSArray *)anArray
    {
    	[objects setArray:anArray];
    }
    
    
    
    -(id)initWithCoder:(NSCoder *)coder
    {
    	if (objects != nil)
    		[objects release];
    	objects = [coder decodeObjectForKey:@"objects"];
    	return self;
    }
    
    -(void)encodeWithCoder:(NSCoder *)coder
    {
    	[coder encodeObject:objects forKey:@"objects"];
    }
    
    - (void) dealloc
    {
    	[objects release];
    	[super dealloc];
    }
    
    @end
    
    and here is the Controller Class:

    Code:
    #import <Cocoa/Cocoa.h>
    #import "ContainerClass.h"
    
    @interface MyController : NSObject {
    	IBOutlet NSTextView *controllerText;
    	
    	NSUserDefaults *prefs;
    	ContainerClass *CC;
    }
    
    @end
    
    @implementation MyController
    
    - (id) init
    {
    	self = [super init];
    	if (self != nil) {
    		prefs = [NSUserDefaults  standardUserDefaults];
    		
    		
    		ContainerClass *originalClass = [[ContainerClass alloc]init];
    		NSData *data = [NSKeyedArchiver archivedDataWithRootObject:originalClass];
    		NSMutableDictionary *defaultPreferences = [NSMutableDictionary dictionary];
    		[defaultPreferences setObject:@"hello" forKey:@"hello1"];
    		[defaultPreferences setObject:data forKey:@"data"];
    		[prefs registerDefaults:defaultPreferences];
    		
    		NSData *extractedData = [prefs objectForKey:@"originalClass"];
    		CC = [NSKeyedUnarchiver unarchiveObjectWithData:extractedData];
    	}
    	return self;
    }
    
    
    -(void)awakeFromNib
    {
    	NSLog([[[CC objects]objectAtIndex:0]valueForKey:@"key"]);
    }
    
    -(void)applicationWillTerminate:(NSNotification *)aNotification
    {
    							 
    }
    
    - (void) dealloc
    {
    	[CC release];
    	[super dealloc];
    }
    
    @end
    The application crashes at startup and the debugger won't even tell me why. All NSCoding functions seem to have been implemented properly. However, the application crashed when the UserDefaults registers the NSData object. Any ideas? I am trying to solve this for many hours...
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Are you compiling on Leopard with GC enabled? If not, you need to fix the memory management problems first. Memory Management Rules
     

Share This Page