【iPhone开发常用代码积累】 归 档

HistoryObject.h

#define kHistoryField1Key @"indexid" #define kHistoryField2Key @"hw" #import <Foundation/Foundation.h> @interface HistoryObject : NSObject <NSCoding>{ NSString *indexid; NSString *hw; } @property (nonatomic, retain) NSString *indexid; @property (nonatomic, retain) NSString *hw; @end

 

 

HistoryObject.m

#import "HistoryObject.h" @implementation HistoryObject @synthesize indexid; @synthesize hw; #pragma mark NSCoding - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:indexid forKey:kHistoryField1Key]; [encoder encodeObject:hw forKey:kHistoryField2Key]; } - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.indexid = [decoder decodeObjectForKey:kHistoryField1Key]; self.hw = [decoder decodeObjectForKey:kHistoryField2Key]; } return self; } - (void)dealloc { [indexid release]; [hw release]; [super dealloc]; } @end

 

 

 

使用:

//保存历史记录 - (void)saveHistory:(NSMutableArray *)sender{ if (sender) { NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:sender forKey:@"HistoryObject"]; [archiver finishEncoding]; [data writeToFile:[self dataFilePath:kHistoryFile] atomically:YES]; [archiver release]; [data release]; } } //取出历史记录 - (void)getHistory { MacmilllanAppDelegate *delegate = (MacmilllanAppDelegate *)[[UIApplication sharedApplication]delegate]; NSString *filePath = [self dataFilePath:kHistoryFile]; if([[NSFileManager defaultManager]fileExistsAtPath:filePath]) { NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; NSMutableArray *array = [unarchiver decodeObjectForKey:@"HistoryObject"]; if (array != nil) { delegate.myHistoryArray = array;//将取出的历史记录放入myHistoryArray数组 [unarchiver finishDecoding]; [unarchiver release]; [data release]; } } }

 

 

具体查看Macmillan

 

 

 

你可能感兴趣的:(iPhone,interface)