IOS中的資料存放區 簡單總結

來源:互聯網
上載者:User

標籤:

  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中的資料存放區 簡單總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.