iOS iPhone 開發中的文件讀寫及數據存儲

參考網址:http://blog.csdn.net/wxq888/article/details/8191076 ios


 數據存儲無疑軟件開發重要課題。本文初學者介紹iphone開發常見文件讀寫 數據庫

iOS的文件存儲採用的是「沙箱機制」,也就是應用程序只能訪問本身的文件目錄,每一個應用程序的數據是獨立的,就像一個一個的沙箱同樣。這種管理方法windows原來塞班那種文件管理方式適合移動平臺。這種方式安全,很大程度上避免了流氓軟件垃圾軟件盜竊資料。 windows

查看模擬器應用程序在mac上的存儲,就能夠了解在iphone上文件是如何組織的。 安全

打開目錄/Users/andy/Library/Application Support/iPhone Simulator/5.1/Applications就會看到模擬器上的程序文件夾,你會看到iphone每一個應用程序都有本身的3個目錄(Document,Library,tmp) app

Documents存放應用程序的數據。 iphone

Library目錄下面還有Preferences和Caches目錄,Preferences目錄存放應用程序的使用偏好,Caches目錄與Documents很相 是能夠存放應用程序的數據。 函數

tmp目錄供應用程序存儲臨時文件。 工具


注意,若是你的設置沒有設置爲查看隱藏目錄,你是看不到的,設置顯示隱藏文件方法:在終端輸入命令:defaults write com.apple.finder AppleShowAllFiles -bool true而後重啓下finder。  atom


在應用程序中得到本身的documents目錄: spa

NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString * documentDirectory = [paths objectAtIndex:0];

在上面的基礎上,得到一個完整的文件路徑和名字:

NSString * file = [documentDirectory stringByAppendingPathComponent:@"file1.txt"];

這就能夠用file來建立,讀取,和寫入文件。


iOS中數據存儲常見的有四種方式: 屬性列表、對象歸檔、ios嵌入式數據庫(SQLite3)和Core Data(蘋果提供的工具)

一,用屬性列表保存數據:

該方法就是針對一些集合類調用writeToFile:atomically方法和initWithContentsOfFile 方法來寫入和讀取數據。

這些集合類包括:NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSData,NSMutableData,NSString,NSMutableString,NSNumber,NSDate。在這裏輸入代碼太麻煩了,沒法識別回車換行,因此這個就不在這裏舉例了。

二,歸檔方法保存自定義對象:

屬性列表方法簡單易用,可是使用有侷限性,就是沒法保存自定義的數據類。要解決這個問題,咱們看歸檔方法。歸檔方法實際就是 用 NSKeyedArchiver 對 自定義類進行編解碼成 NSMutableData 後,再對NSMutableData實行序列化具體的編解碼是由NSCoder實現的。舉個例子就比較容易掌握。

例子中咱們保存4個值和一個名字字典。這些名字包括暱稱,qq網名,微博網名,省份證名字。自定義類以下:

頭文件:

#import <Foundation/Foundation.h> 

@interface BIDFourLines : NSObject

@property (copy, nonatomic) NSString *field1;

@property (copy, nonatomic) NSString *field2;

@property (copy, nonatomic) NSString *field3;

@property (copy, nonatomic) NSString *field4;

@property (copy, nonatomic) NSDictionary * names;

@end


實現文件:

#import "BIDFourLines.h"

#define kField1Key @"Field1"

#define kField2Key @"Field2"

#define kField3Key @"Field3"

#define kField4Key @"Field4"

#define kFieldArrayKey @"FieldArray"

#pragma mark NSCoding

- (void)encodeWithCoder:(NSCoder *)encoder {

 [encoder encodeObject:field1 forKey:kField1Key];

[encoder encodeObject:field2 forKey:kField2Key];

[encoder encodeObject:field3 forKey:kField3Key];

[encoder encodeObject:field4 forKey:kField4Key];

[encoder encodeObject:names forKey:kFieldArrayKey];

}

- (id)initWithCoder:(NSCoder *)decoder {

if (self = [super init]) {

field1 = [decoder decodeObjectForKey:kField1Key];

field2 = [decoder decodeObjectForKey:kField2Key];

field3 = [decoder decodeObjectForKey:kField3Key];

field4 = [decoder decodeObjectForKey:kField4Key];

names = [decoder decodeObjectForKey:kFieldArrayKey];

}

return self;

}

- (id)copyWithZone:(NSZone *)zone {

BIDFourLines *copy = [[[self class] allocWithZone: zone] init];

copy.field1 = [self.field1 copyWithZone:zone];

copy.field2 = [self.field2 copyWithZone:zone];

copy.field3 = [self.field3 copyWithZone:zone];

copy.field4 = [self.field4 copyWithZone:zone];

copy.names = [self.names copyWithZone:zone];

return copy;

}

@end


保存文件代碼:

- (void) saveTofile{

BIDFourLines *fourLines = [[BIDFourLines alloc] init];

fourLines.field1 = field1.text;

fourLines.field2 = field2.text;

fourLines.field3 = field3.text;

fourLines.field4 = field4.text;

fourLines.names = [NSDictionary dictionaryWithObjectsAndKeys:@"andy",@"nickName",@"王勃",@"idName",@"田鼠",@"qqName",@"大力哥",@"weiboName",nil ]; NSMutableData *data = [[NSMutableData alloc] init];

NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

[archiver encodeObject:fourLines forKey:kDataKey];

[archiver finishEncoding];

[data writeToFile:[self dataFilePath] atomically:YES];

}

獲取文件名函數:

- (NSString *)dataFilePath {

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

return [documentsDirectory stringByAppendingPathComponent:@"archive"];

}


- (void) loadFileData{

NSString *filePath = [self dataFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

BIDFourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey];

[unarchiver finishDecoding];

field1.text = fourLines.field1;

field2.text = fourLines.field2;

field3.text = fourLines.field3;

field4.text = fourLines.field4;

NSDictionary * names = fourLines.names;

if(names) {

NSArray * array = [names allKeys];

  for (NSString * value in array) {

     NSLog(@"%@ is %@",value,[names objectForKey:value]);

   }  

  }

 }

}

相關文章
相關標籤/搜索