Return to Snippet

Revision: 26284
at April 22, 2010 12:18 by LeeProbert


Updated Code
/* INTERFACE */

#define	kfavesArray @"favesArray"
#define	kCurrentFave @"currentFave"
#define	kCurrentFaveIndex @"currentFaveIndexPath"

#import <Foundation/Foundation.h>

@class FaveVO;


@interface DataModel : NSObject <NSCoding>
{
	NSMutableArray *favesArray;
	FaveVO *currentFave;
	int faveIndex;
}

@property (nonatomic, retain) NSMutableArray *favesArray;
@property (nonatomic, retain) FaveVO *currentFave;
@property int faveIndex;

-(void)addNewFaveLocation:(FaveVO *)fave;
-(void)createNewFaveVOFromFaveVO:(FaveVO *)fave;
-(void)setFaveLocation:(int)index;
-(void)removeFaveLocation;

@end

/* IMPLEMENTATION */

#import "DataModel.h"
#import "FaveVO.h"

@implementation DataModel

@synthesize favesArray;
@synthesize currentFave;
@synthesize faveIndex;

//------------------------------------------------------
-(id)init
{
	if(self = [super init])
	{
		favesArray = [[NSMutableArray alloc] init];
	}
	
	return self;
}
//--------------------------------------------------
-(void)addNewFaveLocation:(FaveVO *)fave
{
	if(!favesArray) favesArray = [[NSMutableArray alloc] init];
	
	[favesArray insertObject:fave atIndex:0];
	currentFave = [favesArray objectAtIndex:0];
	faveIndex = 0;
	
	NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
}
//--------------------------------------------------
-(void)setFaveLocation:(int)index
{
	currentFave = [favesArray objectAtIndex:index];
	faveIndex = index;
	
	NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
}
//--------------------------------------------------
-(void)removeFaveLocation
{
	currentFave = nil;
	
	NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
}
//--------------------------------------------------
-(void)createNewFaveVOFromFaveVO:(FaveVO *)fave
{
	double lat = fave.lat;
	double lon = fave.lon;
	
	FaveVO *newFaveVO = [[FaveVO alloc] initWithLabel:[NSString stringWithString:fave.label] 
										streetAddress:[NSString stringWithString:fave.streetAddress] 
												 city:[NSString stringWithString:fave.city] 
												state:[NSString stringWithString:fave.state] 
												  zip:[NSString stringWithString:fave.zip] 
												  lat:lat 
												  lon:lon];
	[favesArray addObject:newFaveVO];
	[newFaveVO release];
}
//--------------------------------------------------
-(void)dealloc
{
	[favesArray release];
	[currentFave release];
	[super dealloc];
}
//--------------------------------------------------


//--------------------------------------------------
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)encoder
{
	[encoder encodeObject:favesArray forKey:kfavesArray];
	[encoder encodeObject:currentFave forKey:kCurrentFave];
	[encoder encodeInt:faveIndex forKey:kCurrentFaveIndex];
}
//--------------------------------------------------
- (id)initWithCoder:(NSCoder *)decoder
{
	NSLog(@"DataModel initWithCoder: %@", decoder);
	
	if (self = [super init])
	{
		self.favesArray = [decoder decodeObjectForKey:kfavesArray];
		
		self.faveIndex = [decoder decodeIntForKey:kCurrentFaveIndex];
		self.currentFave = [favesArray objectAtIndex:faveIndex];
	}
	return self;
}

@end

Revision: 26283
at April 22, 2010 11:31 by LeeProbert


Updated Code
/* INTERFACE */

#define	kfavesArray @"favesArray"
#define	kCurrentFave @"currentFave"
#define	kCurrentFaveIndex @"currentFaveIndexPath"

#import <Foundation/Foundation.h>

@class FaveVO;


@interface DataModel : NSObject <NSCoding, NSCopying>
{
	NSMutableArray *favesArray;
	FaveVO *currentFave;
	int faveIndex;
}

@property (nonatomic, retain) NSMutableArray *favesArray;
@property (nonatomic, retain) FaveVO *currentFave;
@property int faveIndex;

-(void)addNewFaveLocation:(FaveVO *)fave;
-(void)createNewFaveVOFromFaveVO:(FaveVO *)fave;
-(void)setFaveLocation:(int)index;
-(void)removeFaveLocation;

@end

/* IMPLEMENTATION */

#import "DataModel.h"
#import "FaveVO.h"

@implementation DataModel

@synthesize favesArray;
@synthesize currentFave;
@synthesize faveIndex;

//------------------------------------------------------
-(id)init
{
	if(self = [super init])
	{
		favesArray = [[NSMutableArray alloc] init];
	}
	
	return self;
}
//--------------------------------------------------
-(void)addNewFaveLocation:(FaveVO *)fave
{
	if(!favesArray) favesArray = [[NSMutableArray alloc] init];
	
	[favesArray insertObject:fave atIndex:0];
	currentFave = [favesArray objectAtIndex:0];
	faveIndex = 0;
	
	NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
}
//--------------------------------------------------
-(void)setFaveLocation:(int)index
{
	currentFave = [favesArray objectAtIndex:index];
	faveIndex = index;
	
	NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
}
//--------------------------------------------------
-(void)removeFaveLocation
{
	currentFave = nil;
	
	NSLog(@"DataModel currentFave: %@ faveIndex: %i", currentFave,faveIndex);
}
//--------------------------------------------------
-(void)createNewFaveVOFromFaveVO:(FaveVO *)fave
{
	double lat = fave.lat;
	double lon = fave.lon;
	
	FaveVO *newFaveVO = [[FaveVO alloc] initWithLabel:[NSString stringWithString:fave.label] 
										streetAddress:[NSString stringWithString:fave.streetAddress] 
												 city:[NSString stringWithString:fave.city] 
												state:[NSString stringWithString:fave.state] 
												  zip:[NSString stringWithString:fave.zip] 
												  lat:lat 
												  lon:lon];
	[favesArray addObject:newFaveVO];
	[newFaveVO release];
}
//--------------------------------------------------
-(void)dealloc
{
	[favesArray release];
	[currentFave release];
	[super dealloc];
}
//--------------------------------------------------


//--------------------------------------------------
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)encoder
{
	[encoder encodeObject:favesArray forKey:kfavesArray];
	[encoder encodeObject:currentFave forKey:kCurrentFave];
	[encoder encodeInt:faveIndex forKey:kCurrentFaveIndex];
}
//--------------------------------------------------
- (id)initWithCoder:(NSCoder *)decoder
{
	NSLog(@"DataModel initWithCoder: %@", decoder);
	
	if (self = [super init])
	{
		self.favesArray = [decoder decodeObjectForKey:kfavesArray];
		
		self.faveIndex = [decoder decodeIntForKey:kCurrentFaveIndex];
		self.currentFave = [favesArray objectAtIndex:faveIndex];
	}
	return self;
}
//------------------------------------------------------------------------
#pragma mark -
#pragma mark NSCopying
- (id)copyWithZone:(NSZone *)zone
{
	DataModel *copy = [[[self class] allocWithZone: zone] init];
	
	copy.favesArray = favesArray;
	copy.currentFave = currentFave;
	copy.faveIndex = faveIndex;
	
	return copy;
}


@end

Revision: 26282
at April 22, 2010 11:29 by LeeProbert


Initial Code
#define	kfavesArray @"favesArray"
#define	kCurrentFave @"currentFave"
#define	kCurrentFaveIndex @"currentFaveIndexPath"

#import <Foundation/Foundation.h>

@class FaveVO;


@interface DataModel : NSObject <NSCoding, NSCopying>
{
	NSMutableArray *favesArray;
	FaveVO *currentFave;
	int faveIndex;
}

@property (nonatomic, retain) NSMutableArray *favesArray;
@property (nonatomic, retain) FaveVO *currentFave;
@property int faveIndex;

-(void)addNewFaveLocation:(FaveVO *)fave;
-(void)createNewFaveVOFromFaveVO:(FaveVO *)fave;
-(void)setFaveLocation:(int)index;
-(void)removeFaveLocation;

@end

Initial URL


Initial Description
Here's a simple object class that has been set up so it can be archived using the NSKeyedUnarchiver and NSKeyedArchiver classes. This process in another snippet.

Initial Title
DataModel Object for archiving

Initial Tags


Initial Language
Objective C