標籤:
1. NSKeyedArchiver(加密形式) 2. plist 3. NSUserDefaults 4. writeToFile 5. SQLite3
==== NSKeyedArchiver ========================================-------CKPerson.h 代碼
@interface CKPerson : NSObject
@property (nonatomic, copy) NSString *name;@property (nonatomic, assign) int age;- (void)encodeWithCoder:(NSCoder *)aCoder;- (id)initWithCoder:(NSCoder *)aDecoder;
@end------- CKPerson.m 代碼- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"];}- (id)initWithCoder:(NSCoder *)aDecoder{ if(self = [super init]) { self.name = [aDecoder decodeObjectForKey:@"name"]; self.age = [aDecoder decodeIntegerForKey:@"age"]; } return self;}---------- CKViewController.m- (IBAction)OnSaveDataClick:(UIButton *)sender { CKPerson *p = [[CKPerson alloc] init]; p.name = @"GoldenKey"; p.age = 21; NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *fileName = filePath = [docPath stringByAppendingPathComponent:@"student.hehe"]; [NSKeyedArchiver archiveRootObject:p toFile:filePath];//儲存資料}- (IBAction)OnGetDataClick:(UIButton *)sender { CKPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];//擷取資料}
==== plist存取 array、dictionary ===================================- (IBAction)OnArraySaveClick:(UIButton *)sender { NSArray *testArray = @[@"111",@"121",@"131",@"141",@"151"]; [testArray writeToFile:self.path4Array atomically:YES];}- (IBAction)OnArrayGetClick:(UIButton *)sender { NSArray *testArray = [NSArray arrayWithContentsOfFile:self.path4Array]; NSLog(@"數組長度為%d",[testArray count]);}- (IBAction)OnDictionarySaveClick:(UIButton *)sender { NSDictionary *dict = @{@"name":@"key",@"age":@12}; [dict writeToFile:self.path4Dict atomically:YES];}- (IBAction)OnDictionaryGetClick:(UIButton *)sender { NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:self.path4Dict]; NSLog(@"name:%@",dict[@"name"]); NSLog(@"age:%@",dict[@"age"]);}
==== NSUserDefaults 程式票號設定 ================================= NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setInteger:1 forKey:@"testInt"]; [userDefaults synchronize]; int i = [userDefaults integerForKey:@"testInt"]; NSLog(@"i=%d",i); //----------- NSString *name = @"GoldenKey"; [userDefaults setObject:name forKey:@"name"]; [userDefaults synchronize]; NSString *nameResult = [userDefaults objectForKey:@"name"]; NSLog(@"%@",nameResult);
==== writeToFile ============================================ --讀取string---------------------------------------------------- NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString *ourDocumentPath =[documentPaths objectAtIndex:0]; //第二步:產生在該路徑下的檔案: NSString *FileName=[ourDocumentPath stringByAppendingPathComponent:@"test.hehe"]; NSString *texts = @"test string"; NSData *data = [texts dataUsingEncoding:NSUTF8StringEncoding]; [data writeToFile:FileName atomically:YES];//將NSData類型對象data寫入檔案,檔案名稱為FileName //讀取方式1 //NSData *dataResult=[NSData dataWithContentsOfFile:FileName options:0 error:NULL];//從FileName中讀取出資料 //NSString *strResult = [[NSString alloc] initWithData:dataResult encoding:NSUTF8StringEncoding]; //讀取方式2 NSString *strResult = [NSString stringWithContentsOfFile:FileName encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",strResult); //--存取dictionary------------------------------------------- NSString *dictFileName = [ourDocumentPath stringByAppendingPathComponent:@"dict.hehe"]; NSDictionary *dictTest = @{@"name":@"GoldenKey",@"age":@24}; NSData *dictData = [NSJSONSerialization dataWithJSONObject:dictTest options:NSJSONWritingPrettyPrinted error:nil]; [dictData writeToFile:dictFileName atomically:YES]; NSData *dataResult = [NSData dataWithContentsOfFile:dictFileName]; NSDictionary *dictResult = [NSJSONSerialization JSONObjectWithData:dataResult options:NSJSONReadingMutableContainers error:nil]; NSLog(@"name = %@",dictResult[@"name"]);==== sqlite3 ========================================================
IOS中的資料存放區 簡單總結