保存数据的一系列方法( 转)

/*=======================================================
NSKeyedArchiver
========================================================*/
NSString *str = @"abc";
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
    
//Save
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = [Path stringByAppendingPathComponent:@"test"];
[NSKeyedArchiver archiveRootObject:Array toFile:filename];
    
str = @"a";
astr = @"";
    
//load
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
str = [arr objectAtIndex:0];
astr =  [arr objectAtIndex:1];
    
NSLog(@"str:%@",str);
NSLog(@"astr:%@",astr);
    
    

/*=======================================================
NSUserDefaults
========================================================*/
NSString *str = @"abc";
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
    
//Save
NSUserDefaults *SaveDefaults = [NSUserDefaults standardUserDefaults];
[SaveDefaults setObject:Array forKey:@"SaveKey"];
    
str = @"a";
astr = @"";
    
//load
Array = [SaveDefaults objectForKey:@"SaveKey"];
str = [Array objectAtIndex:0];
astr = [Array objectAtIndex:1];
NSLog(@"str:%@",str);
NSLog(@"astr:%@",astr);
    
    


/*=======================================================
writeToFile:
========================================================*/
NSString *str = @"abc";
NSString *astr = @"efg";
NSArray *Array = [NSArray arrayWithObjects:str, astr, nil];
    
//Save
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
    NSLog(@"Documents directory not found!");
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"Savedatas.plist"];
[[NSArray arrayWithObjects:Array,nil] writeToFile:appFile atomically:NO];    
    
    
//load
if([[NSFileManager defaultManager] fileExistsAtPath:appFile])
    self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:appFile];        
else
    self.SaveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Savedatas" ofType:@"plist"]];
NSArray *strArray = [self.SaveDataArray objectAtIndex:0];
    
str = [strArray objectAtIndex:0];
astr = [strArray objectAtIndex:1];





//坛子里的,搬过来。。。。。
-(BOOL) writeApplicationData:(NSDictionary *)data  writeFileName:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
        return NO;
    }
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    return ([data writeToFile:appFile atomically:YES]);
}

-(id) readApplicationData:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSDictionary *myData = [[[NSDictionary alloc] initWithContentsOfFile:appFile] autorelease];
    return myData;
}

[/lang]





之前说过的,还有一种保存数据的方法,没有发上来,

现在更新给大家。


用一个简单的例子来说明吧。



//
//  SaveDataInf.m
//  SaveDataUseChar
//
//  Created by Lixf on 10-4-26.
//  Copyright 2010 Lixf. All rights reserved.
//

#import "SaveDataInf.h"


@implementation SaveDataInf
@synthesize SaveData;
@synthesize FontSize, Color, Aligment;

-(id)initDataFromPath{
    NSString *Path = [[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingFormat:@"/Documents/"];
    NSString *LastPath = [Path stringByAppendingFormat:@"Data.inf"];
    if(SaveData != nil)
    {
        [SaveData release];
        SaveData = nil;
    }
    SaveData = [[NSMutableData alloc] initWithContentsOfFile:LastPath];
    unsigned char DataBuffer[10];
    if(SaveData == nil)
    {
        SaveData = [[NSMutableData alloc] initWithLength:1000];
        memset(DataBuffer, 0x0, sizeof(DataBuffer));
        FontSize = 18;
        Color = 0;
        Aligment = 0;
        DataBuffer[0] = FontSize;
        DataBuffer[1] = Color;
        DataBuffer[2] = Aligment;
        [SaveData replaceBytesInRange:NSMakeRange(0, 10) withBytes:DataBuffer];
    }else {
        [SaveData getBytes:DataBuffer range:NSMakeRange(0, 10)];
        FontSize = DataBuffer[0];
        Color = DataBuffer[1];
        Aligment = DataBuffer[2];
    }
    return self;
}

-(BOOL)writeDataToPath{
    NSString *Path = [[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingFormat:@"/Documents/"];
    NSString *LastPath = [Path stringByAppendingFormat:@"Data.inf"];
    BOOL isSuccess = [SaveData writeToFile:LastPath atomically:YES];
    if(isSuccess == YES)
    {
        return YES;
    }else {
        return NO;
    }

}

-(void)setFontSize:(char)size{
    unsigned char DataBuffer[10];
    FontSize = size;
    DataBuffer[0] = size;
    [SaveData replaceBytesInRange:NSMakeRange(0, 1) withBytes:DataBuffer];
}

-(void)setFontColor:(char)color{
    unsigned char DataBuffer[10];
    Color = color;
    DataBuffer[0] = color;
    [SaveData replaceBytesInRange:NSMakeRange(1, 1) withBytes:DataBuffer];
}

-(void)setFontAligment:(char)aligment{
    unsigned char DataBuffer[10];
    Aligment = aligment;
    DataBuffer[0] = aligment;
    [SaveData replaceBytesInRange:NSMakeRange(2, 1) withBytes:DataBuffer];
}

- (void)dealloc {
   if(SaveData != nil)
   {
       [SaveData release];
       SaveData = nil;
   }
    [super dealloc];
}
@end

注:忙里偷闲。。。我只写了使用的方法。。一些细微细节大家使用的时候要注意。。。
[ 此帖被angellixf在2010-05-19 08:55重新编辑 ]
附件: SaveDataUseChar.zip (24 K) 下载次数:220
源链接:http://www.cocoachina.com/bbs/read.php?tid-9414.html

你可能感兴趣的:(方法)