Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-10-2011, 06:20 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default Saving/Retrieving NSMutableArray after relaunch

Here's what I have so far which works to an extent:

CustomClass.h:
Code:
#import <Foundation/Foundation.h>


@interface CustomClass : NSObject <NSCoding> {
    NSString *name;
    float value1;
    
    int value2;
}

-(NSString*)name;
-(float)value1;
-(int)value2;

-(void)setName:(NSString *)input;
-(void)setValue1:(float)input;
-(void)setValue2:(int)input;

@end
CustomClass.m:
Code:
#import "CustomClass.h"


@implementation CustomClass

-(id)initWithCoder:(NSCoder *)coder {
	self = [[CustomClass alloc] init];
    if (self != nil) {
		self.name = [coder decodeObjectForKey:@"name"];
	}
	
	return self;
}

-(void)encodeWithCoder:(NSCoder *)coder {
	[coder encodeObject:name forKey:@"name"];
	
}

-(int)value2 {
    return value2;
}

-(float)value1 {
    return value1;
}

-(NSString*)name {
    return name;
}

-(void)setValue1:(float)input {
    value1 = input;
}

-(void)setValue2:(int)input {
    value2 = input;
}

-(void)setName:(NSString *)input {
    [input retain];
    [name release];
    name = input;
}

-(void)dealloc {
    [super dealloc];
    
    [name release];
}

@end
Save Array:
Code:
   NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    [currentDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:dataArray] forKey:@"savedArray"];
    [currentDefaults synchronize];
Retrieve Array:
Code:
    dataArray = [[NSMutableArray alloc] init];
	
    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedArray"];
    if (dataRepresentingSavedArray != nil) {
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
        if(oldSavedArray != nil) {
            dataArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
        }
        else {
            dataArray = [[NSMutableArray alloc] init];
        }
    }
This works to save and load an array of my custom objects to a table view. The only problem: the custom objects value's have been reset when I relaunch. I want to maintain the values of my custom objects (i.e. the name, value1, and value2). Can someone tell me where I went wrong? Thanks.
Whitehk is offline   Reply With Quote
Old 05-10-2011, 06:26 PM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

In your initWithCoder and encodeWithCoder, you need to include all of your objects.
Domele is offline   Reply With Quote
Old 05-10-2011, 06:33 PM   #3 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
In your initWithCoder and encodeWithCoder, you need to include all of your objects.
A float and integer aren't objects. Are they? Whenever I try to include them in the initWithCoder and encodeWithCoder methods, it says, "assigning to 'float' from incompatible type 'id'" and "incompatible pointer to integer conversion assigning to 'int' from 'id'". Even if that's the case, the name string is also lost even though I included this in the initWithCoder and encodeWithCoder methods.
Whitehk is offline   Reply With Quote
Old 05-10-2011, 06:54 PM   #4 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Sorry I didn't use the right word, all the data that you want to save must go into those methods. I believe there are specific methods for floats and int. Probably [coder decodeFloat...] and [coder encodeFloat...].
Domele is offline   Reply With Quote
Old 05-10-2011, 07:06 PM   #5 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Sorry I didn't use the right word, all the data that you want to save must go into those methods. I believe there are specific methods for floats and int. Probably [coder decodeFloat...] and [coder encodeFloat...].
Thank you! That fixed it! I knew it was a stupid mistake like that... Usually is. XD
Whitehk is offline   Reply With Quote
Reply

Bookmarks

Tags
custom objects, nsmutablearray, nsuserdefaults, retrieve data, save data

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 356
7 members and 349 guests
doffing81, dre, iOS.Lover, MikaelBartlett, oztemel, PlutoPrime, thephotographer
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:01 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0