iOS中的數(shù)據(jù)持久化方式勉痴,基本上有以下四種: 屬性列表涨薪、對(duì)象歸檔待榔、SQLite3和Core Data
1.屬性列表涉及到的主要類:NSUserDefaults,一般 [NSUserDefaults? standardUserDefaults?]就夠用了
@interface User : NSObject
@property (nonatomic, assign) NSInteger userID;
@property (nonatomic, copy) NSString *name;
@end使用方法
1).分開存取
// 存[[NSUserDefaults standardUserDefaults] setInteger:userID forKey:@”userID”];?
[[NSUserDefaults standardUserDefaults] setObject:name forKey:@”name”];
// 取NSInteger uId = [[[NSUserDefaults standardUserDefaults] integerValueForKey:@”userID”];
NSString* name = [[NSUserDefaults standardUserDefaults] stringForKey:@”name”];
?2).按對(duì)象存取
// 存[[NSUserDefaults standardUserDefaults] setObject:self forKey:@”user”];
// 取User* u = [[NSUserDefaults standardUserDefaults] objectForKey”@”user”];
2.對(duì)象歸檔要使用對(duì)象歸檔,對(duì)象必須實(shí)現(xiàn)NSCoding協(xié)議.大部分Object C對(duì)象都符合NSCoding協(xié)議,也可以在自定義對(duì)象中實(shí)現(xiàn)NSCoding協(xié)議,要實(shí)現(xiàn)NSCoding協(xié)議,實(shí)現(xiàn)兩個(gè)方法:
- (void) encodeWithCoder:(NSCoder *)encoder 與 -(void)initWithCoder:(NSCoder *)encoder同時(shí),建議對(duì)象也同時(shí)實(shí)現(xiàn)NSCopying協(xié)議,該協(xié)議允許復(fù)制對(duì)象,要實(shí)現(xiàn)NSCopying協(xié)議須實(shí)現(xiàn) -(id)copyWithZone:(NSZone *)zone 方法 屁使。@interface User : NSObject@property (nonatomic, assign) NSInteger userID;
@property (nonatomic, copy) NSString *name;
?@end
@implementation User
// 以下兩個(gè)方法一定要實(shí)現(xiàn)减细,不然在調(diào)用的時(shí)候會(huì)crash
- (void)encodeWithCoder:(NSCoder *)aCoder;
{
// 這里放置需要持久化的屬性
[aCoder encodeObject:[NSNumber numberWithInteger:self.userID] forKey:@”userID”];
[aCoder encodeObject:self.name forKey:@"name"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [self init])
{
// 這里務(wù)必和encodeWithCoder方法里面的內(nèi)容一致匆瓜,不然會(huì)讀不到數(shù)據(jù)
self.userID = [[aDecoder decodeObjectForKey:@"userID"] integerValue];
self.name = [aDecoder decodeObjectForKey:@"name"];
}
return self;
}
// 使用方法
+ (BOOL)save {
NSError *error = nil;
// 確定存儲(chǔ)路徑,一般是Document目錄下的文件
NSString* fileName = [self getFileName];
NSString* filePath = [self getFilePath];
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@”創(chuàng)建用戶文件目錄失敗”);
return NO;
}
return [NSKeyedArchiver archiveRootObject:self toFile:[fileName:userId]];
}
?@end
3.SQLite3?
SQLite是一個(gè)開源的嵌入式關(guān)系數(shù)據(jù)庫(kù),它在2000年由D. Richard Hipp發(fā)布驮吱,它的減少應(yīng)用程序管理數(shù)據(jù)的開銷茧妒,SQLite可移植性好,很容易使用左冬,很小桐筏,高效而且可靠。
SQLite嵌入到使用它的應(yīng)用程序中拇砰,它們共用相同的進(jìn)程空間梅忌,而不是單獨(dú)的一個(gè)進(jìn)程。從外部看除破,它并不像一個(gè)RDBMS牧氮,但在進(jìn)程內(nèi)部,它卻是完整的瑰枫,自包含的數(shù)據(jù)庫(kù)引擎歪今。 嵌入式數(shù)據(jù)庫(kù)的一大好處就是在你的程序內(nèi)部不需要網(wǎng)絡(luò)配置利虫,也不需要管理笔喉。因?yàn)榭蛻舳撕头?wù)器在同一進(jìn)程空間運(yùn)行忽孽。SQLite 的數(shù)據(jù)庫(kù)權(quán)限只依賴于文件系統(tǒng),沒有用戶帳戶的概念盯另。SQLite 有數(shù)據(jù)庫(kù)級(jí)鎖定性含,沒有網(wǎng)絡(luò)服務(wù)器。它需要的內(nèi)存鸳惯,其它開銷很小商蕴,適合用于嵌入式設(shè)備。你需要做的僅僅是把它正確的編譯到你的程序悲敷。
關(guān)于SQLite的開發(fā)資料較多究恤,這里不再細(xì)說。只是建議不直接操作SQLite庫(kù)后德,而是采用一些開源的第三方庫(kù)來進(jìn)行操作部宿。比如:
FMDB:https://github.com/ccgus/fmdb.git
對(duì)SQLite都做了不錯(cuò)的封裝。
?
4.?Core Data?
Core Data本質(zhì)上是使用SQLite保存數(shù)據(jù)瓢湃,但是它不需要編寫任何SQL語句理张。
要使用Core Data,需要在Xcode中的數(shù)據(jù)模型編輯器中設(shè)計(jì)好各個(gè)實(shí)體以及定義好他們的屬性和關(guān)系绵患。之后雾叭,通過操作這些對(duì)象,結(jié)合Core Data完成數(shù)據(jù)的持久化:
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;
NSString *fieldName = [NSString stringWithFormat:@"test%d", i];
UITextField *theField = [self valueForKey:fieldName];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//創(chuàng) 建描述語句落蝙,需求Line對(duì)象织狐。類似于在數(shù)據(jù)庫(kù)中限定為L(zhǎng)ine表暂幼。?
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
[request setEntity:entityDescription];
//創(chuàng)建限制性語句,類似于SQL語句中的 where lineNum = i?
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i];
[request setPredicate:pred];
NSManagedObject *theLine = nil;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects == nil){
NSLog(@”There was an error!”);
// Do whatever error handling is appropriate
}
if ([objects count] > 0){ //如果符合條件的object存在移迫,則取出
theLine = [objects objectAtIndex:0];
}
else { //如果不存在旺嬉,則插入一個(gè)新的.
theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line"
inManagedObjectContext:context];
[theLine setValue:[NSNumber numberWithInt:i] forKey:@”lineNum”]; //設(shè)置這個(gè)object的屬性,coredata會(huì)自動(dòng)將其寫入sqlite
[theLine setValue:theField.text forKey:@"lineText"];
[request release];
}
}
下面是其取數(shù)據(jù)的過程厨埋。
Core_Data_PersistenceAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects == nil)
{
NSLog(@”There was an error!”);
// Do whatever error handling is appropriate
}
//每一個(gè)對(duì)象在CoreData中都表示為一個(gè)NSManagedObject對(duì)象(類似于數(shù)據(jù)庫(kù)表中的每一行),他的屬性通過鍵/值 方式獲取?
for (NSManagedObject *oneObject in objects)
{
NSNumber *lineNum = [oneObject valueForKey:@"lineNum"];
NSString *lineText = [oneObject valueForKey:@"lineText"];
}
[request release];?
====================================
本文原鏈接:http://www.cocoachina.com/bbs/read.php?tid=137443
====================================