數(shù)據(jù)存儲

1> XML屬性列表歸檔:.plist?

2> 偏好設(shè)置:NSUserDefault

3> 歸檔:NSKeydeArchiver

4> 關(guān)系型數(shù)據(jù)庫:Sqlite 3

5> 對象型的數(shù)據(jù)庫:Core Data

沙盒:文件系統(tǒng)目錄,iOS應(yīng)用程序都是獨立的沙盒;

Documents: 如:數(shù)據(jù)庫文件放置這里;

Library: Caches, Preferences 如:圖片等緩沖文件放置這里,會自動刪除掉的空間;

tmp:

推介工具軟件:SimPholders

- (void)initWithOther

{

// 根目錄

NSString *home = NSHomeDirectory();

NSLog(@"home : %@", home);

// 獲取Document,創(chuàng)建一個bank.plist文件

NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

NSString *filePath = [document stringByAppendingPathComponent:@"bank.plist"];

NSLog(@"filePath : %@", filePath);

// 字典寫入

NSDictionary *dic = @{@"name" : @"yulong", @"count" : @"6", @"id" : @"123456", @"type" : @"農(nóng)行"};

[dic writeToFile:filePath atomically:YES];

// 讀文件

NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:filePath];

NSLog(@"readDic : %@", readDic);

}

- (void)writeData

{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:@"yulong" forKey:@"name"];

[defaults setInteger:1 forKey:@"money"];

[defaults setDouble:1.78 forKey:@"height"];

[defaults synchronize];

}

- (void)readData

{

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *name = [defaults objectForKey:@"name"];

NSUInteger money = [defaults integerForKey:@"money"];

double height = [defaults doubleForKey:@"height"];

NSLog(@"name : %@, money : %lu, height : %f", name, (unsigned long)money, height);

}

偏好設(shè)置:保存應(yīng)用程序的配置信息;

- (void)writeArchive

{

Student *student = [[Student alloc] init];

student.name = @"yulong";

student.age = 8;

student.className = @"小班一班";

student.classID = @"1234560798";

NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *filePath = [document stringByAppendingPathComponent:@"student.txt"];

NSLog(@"filePath : %@", filePath);

[NSKeyedArchiver archiveRootObject:student toFile:filePath];

}

- (void)readArchive

{

NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *filePath = [document stringByAppendingPathComponent:@"student.txt"];

NSLog(@"filePath : %@", filePath);

Student *student = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

NSLog(@"%@ %lu %@ %@", student.name, (unsigned long)student.age, student.className, student.classID);

}

自定義對象保存到文件中,必須實現(xiàn)<NSCoding>協(xié)議;

- (instancetype)initWithCoder:(NSCoder *)aDecoder

{

NSLog(@"%s", __func__);

self = [super init];

if (self)

{

_name = [aDecoder decodeObjectForKey:@"name"];

_age = [aDecoder decodeIntegerForKey:@"age"];

_className = [aDecoder decodeObjectForKey:@"className"];

_classID = [aDecoder decodeObjectForKey:@"classID"];

}

return self;

}

- (void)encodeWithCoder:(NSCoder *)aCoder

{

NSLog(@"%s", __func__);

[aCoder encodeObject:_name forKey:@"name"];

[aCoder encodeInteger:_age forKey:@"age"];

[aCoder encodeObject:_className forKey:@"className"];

[aCoder encodeObject:_classID forKey:@"classID"];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容