Posts Tagged ‘archive’

[iOS] how to archive and unarchive custom struct and array.

Tuesday, December 14th, 2010

I saw this page.
>> How to store custom objects with struct in Coredata

MyData.h

#import <UIKit/UIKit.h>

typedef struct{
	float data1;
	int data2;
} MyStruct;

@interface MyData : NSObject <NSCoding>{
	NSString *str;
	int count;
	float myf;
	MyStruct mystruct2;
	CGPoint pt;
	int *array1;
}
@property (nonatomic, retain) NSString *str;
@property (assign) int count;
@property (assign) float myf;
@property (assign) CGPoint pt;
@property (assign) MyStruct mystruct2;
@property (assign) int *array1;
@end

MyData.mm

#import "MyData.h"

@implementation MyData
@synthesize str, count, myf;
@synthesize pt;
@synthesize mystruct2;
@synthesize array1;

- (void)encodeWithCoder:(NSCoder *)encoder
{
	[encoder encodeObject:str forKey:@"str"];
	[encoder encodeInt:count forKey:@"mycount"];
	[encoder encodeFloat:myf forKey:@"myfloat"];
	NSValue *value = [NSValue valueWithCGPoint:pt];
	[encoder encodeObject:value forKey:@"mypt"];
	[encoder encodeObject:[NSData dataWithBytes:&mystruct2 length:sizeof(MyStruct)] forKey:@"mystruct"];
	[encoder encodeObject:[NSData dataWithBytes:array1 length:sizeof(int) * 3] forKey:@"myarr"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
	if (self = [super init]) {
		self.str = [decoder decodeObjectForKey:@"str"];	
		self.count = [decoder decodeIntForKey:@"mycount"];
		self.myf = [decoder decodeFloatForKey:@"myfloat"];
		NSValue *value = [decoder decodeObjectForKey:@"mypt"];
		[value getValue:&pt];		
		NSData *data = [decoder decodeObjectForKey:@"mystruct"];
		[data getBytes:&mystruct2 length:sizeof(MyStruct)];
		NSData *data2 = [decoder decodeObjectForKey:@"myarr"];
		NSLog(@"data2 = %@", data2);
		array1 = new int[3];
		[data2 getBytes:array1 length:sizeof(int) * 3];
	}
	return self;
}

- (void)dealloc
{
	if (array1) {		
		delete [] array1;
		NSLog(@"delete");
	}
	[super dealloc];
}
@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ArchiveTest2ViewController : UIViewController {

}
- (IBAction)savePressed;
- (IBAction)loadPressed;
@end

ViewController.mm

#import "ArchiveTest2ViewController.h"
#import "MyData.h"

@implementation ArchiveTest2ViewController

- (NSString *)dataFilePath
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDir = [paths objectAtIndex:0];
	return [documentsDir stringByAppendingPathComponent:@"archive"];
}

- (IBAction)savePressed
{
	MyData *mydata = [[MyData alloc] init];
	mydata.str = @"My Data !!";
	mydata.count = 12;
	mydata.myf = 0.35;

	MyStruct mystruct;
	mystruct.data1 = 0.98;
	mystruct.data2 = 888;
	mydata.mystruct2 = mystruct;

	CGPoint pt = CGPointMake(13.0, 12.5);
	mydata.pt = pt;
	
	int *arr = new int[3];
	arr[0] = 183;
	arr[1] = 283;
	arr[2] = 200;
	mydata.array1 = arr;

	NSMutableData *data = [[NSMutableData alloc] init];
	NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
	[archiver encodeObject:mydata forKey:@"Data"];
	[archiver finishEncoding];
	[data writeToFile:[self dataFilePath] atomically:YES];
	
	[mydata release];
	[archiver release];
	[data release];
}

- (IBAction)loadPressed
{
	NSString *filePath = [self dataFilePath];
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
		NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
		NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
		MyData *mydata = [unarchiver decodeObjectForKey:@"Data"];
		[unarchiver finishDecoding];
		
		NSLog(@"str = %@", mydata.str);
		NSLog(@"count = %d", mydata.count);
		NSLog(@"float = %f", mydata.myf);
		NSLog(@"pt = %@", NSStringFromCGPoint(mydata.pt));
		NSLog(@"myst = %f, %d", mydata.mystruct2.data1, mydata.mystruct2.data2);
		int *arr = mydata.array1;
		NSLog(@"array = %d, %d, %d", arr[0], arr[1], arr[2]);
		[unarchiver release];
		[data release];
	}
}



- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


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

@end