數據存儲

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

2> 偏好設置:NSUserDefault

3> 歸檔:NSKeydeArchiver

4> 關系型數據庫:Sqlite 3

5> 對象型的數據庫:Core Data

沙盒:文件系統目錄,iOS應用程序都是獨立的沙盒;

Documents: 如:數據庫文件放置這里;

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

tmp:

推介工具軟件:SimPholders

- (void)initWithOther

{

// 根目錄

NSString *home = NSHomeDirectory();

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

// 獲取Document,創建一個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" : @"農行"};

[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);

}

偏好設置:保存應用程序的配置信息;

- (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);

}

自定義對象保存到文件中,必須實現<NSCoding>協議;

- (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"];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容