資料持久化,簡單對象寫入本地,複雜物件寫入本地

來源:互聯網
上載者:User

標籤:additional   應用程式   interface   loading   import   

#import "MainViewController.h"#import "Student.h"@interface MainViewController ()@end@implementation MainViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        //資料持久化  ,本地存貯    //1.通過代碼找到當前應用程式的沙箱路徑    //函數:  參數1:要找沙箱中的哪個檔案夾    //參數2:在系統的什麼地方去找檔案夾(在IOS系統下,只能用NSUserDomainMask = 1)    //參數3:是否是一個絕對路徑//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//    //    NSLog(@"documents的路徑:%@",paths);//        //三個檔案夾的作用    //1.Documents檔案夾:存貯使用者的設定,使用者需要儲存的一些資料       //限制:如果你在iCloud服務,會把Documents檔案中所有的資料都上傳到伺服器上.iCloud伺服器提供空間是有限的(5G),不要賊這個檔案夾儲存音頻/視頻/體積比較大的問價;    //2.Library:  --Caches;快取檔案夾,存貯系統/使用者的一些快取檔案(比如:txt/圖片/視頻/音頻...,清楚緩衝就是刪除這個檔案夾)                //Perferences檔案夾:是給開發人員使用的喜好設定檔案夾,用來存貯應用執行需要的一些資料.    //3.tmp檔案夾:存貯臨時檔案 ,在版本更新的時候會被刪除掉所有內容.    //4.應用程式的可執行檔(.app),包含這個應用程式執行的所有代碼,資源,基本設定,(完全不能被修改)(NSBundle)        //擷取app中一個圖片路徑    //參數1:資源的檔案名稱    //參數2:資源的檔案類型//    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"1-121126113549" ofType:@"jpg"];//    NSLog(@"%@",imagePath);//    //    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];    //兩種顯示圖片的方式;//    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];//    UIImage *image1 = [UIImage imageNamed:@"1-121126113549.jpg"];//    imageView.image = [UIImage imageWithContentsOfFile:imagePath];//    [self.view addSubview:imageView];        //imageNamed載入圖片,並且把image緩衝到記憶體裡面,    //imageWithContentsOfFile是只顯示圖片,但不加到記憶體中。    //所以載入大量圖片的時候用imageWithContentsOfFile比較好,記憶體不會變大。            //--------------簡單的對象寫入本地--_-_------------//    //NSString,NSArray,NSDictionary,,,這種系統自己提供資料容器類產生的對象,稱為簡單對象        //NSString字串寫入本地    NSString *string = @"沒事兒不要老請假!";        //系統提供的直接寫入本地的方法    //拼接檔案路徑    //擷取libary的檔案夾路徑    NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];        NSString *strPath = [libPath stringByAppendingString:@"/string.txt"];    NSLog(@"字串檔案的路徑%@",strPath);        //參數1:檔案的路徑  參數2:是否對存貯的過程進行保護  參數3:對文本的編碼方式(utf8)  參數4:錯誤資訊()    NSError *error = nil;    [string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:&error];    if (error != nil) {        NSLog(@"錯誤資訊: %@",error);    }        //從本地讀取檔案資料    NSString *readStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];    NSLog(@"%@", readStr);                //數組的讀寫    NSArray *array = [NSArray arrayWithObjects:@"aa",@"bb", @"cc", @"dd", @"ee", @"ff", nil];            //數組對象的路徑拼接    NSString *arrayPath = [libPath stringByAppendingPathComponent:@"array.plist"];    [array writeToFile:arrayPath atomically:YES];    //讀取    NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayPath];    NSLog(@"%@",readArray);        //使用NSUserDefaults類進行本機存放區    //這是一個可以儲存在本地的系統的字典    //使用一個單例方法 建立這個對象    //單例  :  設計模式:在一個程式運行中,只會產生一個對象    NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];    //使用    [userdefaults setObject:@"alksdjflkasd" forKey:@"key"];    //立即寫入本地    [userdefaults synchronize];    //顯示  (判斷第一次登陸)    NSLog(@"%@", [userdefaults objectForKey:@"key"]);        //判斷使用者是否是第一次使用應用    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstLogin"] == nil) {        //如果第一次使用        [[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"isFirstLogin"];        [[NSUserDefaults standardUserDefaults] synchronize];    }else{        //不是第一次    }        //4.複雜物件寫入本地        Student *stu = [[Student alloc] init];    stu.name = @"maidong";    stu.sex = @"nv";    stu.address = @"liaoningdalian";        //歸檔類 ,把一個複雜物件寫入本地(只能實現有NSCoding協議的)    //參數1:你要儲存的對象(必須是實現<NSCoding>協議)    //參數2:儲存路徑        NSString *stuPath = [libPath stringByAppendingString:@"student.suiyi"];        [NSKeyedArchiver archiveRootObject:stu toFile:stuPath];    [stu release];    //讀取    //反歸檔類    Student *stuRead = [NSKeyedUnarchiver unarchiveObjectWithFile:stuPath];    NSLog(@"姓名:%@,性別:%@,住址:%@",stuRead.name,stuRead.sex,stuRead.address);        //儲存複雜物件的流程    //1.建立類,實現NSCoding協議,對所有的屬性進行編碼的解碼    //2.利用歸檔類(NSKeyedArchinver)對一個類的對象進行歸檔寫入本地    //3利用反歸檔類(NSKeyedUnarchiver)對一個路徑下的歸檔檔案進行反歸檔,產生一個新的對象        }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end


本文出自 “小劉_Blog” 部落格,請務必保留此出處http://liuyafang.blog.51cto.com/8837978/1553016

資料持久化,簡單對象寫入本地,複雜物件寫入本地

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.