基本数据持久性

获取Documents目录

[cpp]  view plain copy
  1. NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  2. NSString *documentsDirectory = [path objectAtIndex:0];  
  3.       
  4. NSString *filename = [documentsDirectory stringByAppendingFormat:@"thefFile.txt"];  

获取tmp目录

[cpp]  view plain copy
  1. NSString *tempPath = NSTemporaryDirectory();  
  2. NSString *tempFile = [tempFile stringByAppendingFormat:@"tempFile.txt"];  

属性列表序列化

    许多应用程序都使用了属性列表,比如使用属性列表来指定程序首选项,只要字典或数组仅包含特定可序列化的对象,就可以将NSDictionary和NSarray实例写入属性列表及从属性列表创建他们。序列化对象已被转换为字节流,以便存储到文件中,或通过网络进行传输。尽管可以让任何对象可序列化,但是只能将某些对象放置到某个集合类(如NSDictionary或NSArray)中,然后使用该集合的writeToFile方法将他们存储到属性列表

    如果你打算使用属性列表持久保存程序数据,则可以使用NSArray或NDDirectionary容纳需要持久化的数据,假设你放到NSArray或NSDirectionary的所有对象都是可序列化的对象,则可以通过字典或数组实例调用writeToFile:atnomically:方法来编写属性列表

[cpp]  view plain copy
  1. [myArray writeToFile:@"/some/file/location/output.plist" atomically:YES];  

1、新建single view application

2、打开viewController.xib,添加控件如图


2、修改控制器

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. #define kFilename @"data.plist"  
  3.   
  4. @interface ViewController : UIViewController  
  5. {  
  6.     IBOutlet UITextField *field1;  
  7.     IBOutlet UITextField *field2;  
  8.     IBOutlet UITextField *field3;  
  9.     IBOutlet UITextField *field4;  
  10. }  
  11.   
  12. @property (nonatomic,retain) UITextField *field1;  
  13. @property (nonatomic,retain) UITextField *field2;  
  14. @property (nonatomic,retain) UITextField *field3;  
  15. @property (nonatomic,retain) UITextField *field4;  
  16.   
  17. - (NSString *) dataFilePath;  
  18. - (void) applicationWillTerminate:(NSNotification *) notification;  
  19.   
  20. @end  

[cpp]  view plain copy
  1. @implementation ViewController  
  2. @synthesize field1;  
  3. @synthesize field2;  
  4. @synthesize field3;  
  5. @synthesize field4;  
  6.   
  7. - (NSString *)dataFilePath  
  8. {  
  9.     NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  10.     NSString *documentsDirectory = [path objectAtIndex:0];  
  11.     return [documentsDirectory stringByAppendingFormat:@"thefFile.txt"];  
  12. }  
  13.   
  14. -(void) applicationWillTerminate:(NSNotification *) notifaction  
  15. {  
  16.     NSMutableArray *array = [[NSMutableArray alloc ] init];  
  17.     [array addObject:field1.text];  
  18.     [array addObject:field2.text];  
  19.     [array addObject:field3.text];  
  20.     [array addObject:field4.text];  
  21.     [array writeToFile:[self dataFilePath] atomically:YES];  
  22.     [array release];  
  23. }  
  24.   
  25. - (void) viewDidLoad  
  26. {  
  27.     NSString *filePath = [self dataFilePath];  
  28.     if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {  
  29.         NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];  
  30.         field1.text = [array objectAtIndex:0];  
  31.         field2.text = [array objectAtIndex:1];  
  32.         field3.text = [array objectAtIndex:2];  
  33.         field4.text = [array objectAtIndex:3];  
  34.         [array release];  
  35.     }  
  36.       
  37.     UIApplication *app = [UIApplication sharedApplication];  
  38.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];  
  39.     [super viewDidLoad];  
  40. }  
  41.   
  42. - (void)dealloc {  
  43.     [field1 release];  
  44.     [field2 release];  
  45.     [field3 release];  
  46.     [field4 release];  
  47.     [super dealloc];  
  48. }  
  49. @end  

3、重新打开.xib 按下Control的同时,将File's Owner图标拖放到第一个文本字段,选择field1,第二个连接到field2 ......

对象归档

[csharp]  view plain copy
  1. #define kField1Key @"Field1"  
  2. #define kField2Key @"Field2"  
  3. #define kField3Key @"Field3"  
  4. #define kField4Key @"Field4"  
  5.  
  6. #import <UIKit/UIKit.h>  
  7.   
  8.   
  9. @interface FourLines : NSObject <NSCoding,NSCopying>  
  10. {  
  11.     NSString *field1;  
  12.     NSString *field2;  
  13.     NSString *field3;  
  14.     NSString *field4;  
  15. }  
  16.   
  17. @property (nonatomic,retain) NSString *field1;  
  18. @property (nonatomic,retain) NSString *field2;  
  19. @property (nonatomic,retain) NSString *field3;  
  20. @property (nonatomic,retain) NSString *field4;  

[csharp]  view plain copy
  1. @implementation FourLines  
  2. @synthesize field1;  
  3. @synthesize field2;  
  4. @synthesize field3;  
  5. @synthesize field4;  
  6.   
  7. - (void) encodeWithCoder:(NSCoder *)aCoder  
  8. {  
  9.     [aCoder encodeObject:field1 forKey:kField1Key];  
  10.     [aCoder encodeObject:field2 forKey:kField2Key];  
  11.     [aCoder encodeObject:field3 forKey:kField3Key];  
  12.     [aCoder encodeObject:field4 forKey:kField4Key];  
  13. }  
  14.   
  15. - (id) initWithCoder:(NSCoder *)aDecoder  
  16. {  
  17.     if (self = [super init]) {  
  18.         self.field1 = [aDecoder decodeObjectForKey:kField1Key];  
  19.         self.field2 = [aDecoder decodeObjectForKey:kField2Key];  
  20.         self.field3 = [aDecoder decodeObjectForKey:kField3Key];  
  21.         self.field4 = [aDecoder decodeObjectForKey:kField4Key];  
  22.     }  
  23.     return self;  
  24. }  
  25.   
  26. - (id) copyWithZone:(NSZone *)zone  
  27. {  
  28.     FourLines *copy = [[[self class] allocWithZone:zone] init];  
  29.     field1 = [self.field1 copy];  
  30.     field2 = [self.field2 copy];  
  31.     field3 = [self.field3 copy];  
  32.     field4 = [self.field4 copy];  
  33.       
  34.     return copy;  
  35. }  
  36.   
  37. @end  

[csharp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. #define kFilename @"archive"  
  3. #define kDataKey @"Data"  
  4.   
  5. @interface PersistenceViewController : UIViewController  
  6. {  
  7.     IBOutlet UITextField *field1;  
  8.     IBOutlet UITextField *field2;  
  9.     IBOutlet UITextField *field3;  
  10.     IBOutlet UITextField *field4;  
  11. }  
  12.   
  13. @property (nonatomic,retain) UITextField *field1;  
  14. @property (nonatomic,retain) UITextField *field2;  
  15. @property (nonatomic,retain) UITextField *field3;  
  16. @property (nonatomic,retain) UITextField *field4;  
  17.   
  18. - (NSString *) dataFilePath;  
  19. - (void) applicationWillTerminate:(NSNotification *) notifacation;  
  20.   
  21. @end  

[csharp]  view plain copy
  1. #import "ViewController.h"  
  2. #import "FourLines.h"  
  3.   
  4. @implementation PersistenceViewController  
  5.   
  6. @synthesize field1;  
  7. @synthesize field2;  
  8. @synthesize field3;  
  9. @synthesize field4;  
  10.   
  11. - (NSString *) dataFilePath  
  12. {  
  13.     NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  14.     NSString *documentsDirectory = [path objectAtIndex:0];  
  15.       
  16.     return [documentsDirectory stringByAppendingPathComponent:kFilename];  
  17. }  
  18.   
  19. - (void) applicationWillTerminate:(NSNotification *) notifacation  
  20. {  
  21.     FourLines *fourLines = [[FourLines alloc] init];  
  22.     fourLines.field1 = field1.text;  
  23.     fourLines.field2 = field2.text;  
  24.     fourLines.field3 = field3.text;  
  25.     fourLines.field4 = field4.text;  
  26.       
  27.     NSMutableData *data = [[NSMutableData alloc] init];  
  28.     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];  
  29.     [archiver encodeObject:fourLines forKey:kDataKey];  
  30.     [archiver finishEncoding];  
  31.     [data writeToFile:[self dataFilePath] atomically:YES];  
  32.     [fourLines release];  
  33.     [archiver release];  
  34.     [data release];  
  35. }  
  36.   
  37. - (void) viewDidLoad  
  38. {  
  39.     NSString *filePath = [self dataFilePath];  
  40.     if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {  
  41.         NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];  
  42.         NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  43.         FourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey];  
  44.         field1.text = fourLines.field1;  
  45.         field2.text = fourLines.field2;  
  46.         field3.text = fourLines.field3;  
  47.         field4.text = fourLines.field4;  
  48.           
  49.         [unarchiver release];  
  50.         [data release];  
  51.           
  52.     }  
  53.       
  54.     UIApplication *app = [UIApplication sharedApplication];  
  55.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];  
  56.     [super viewDidLoad];  
  57. }  
  58.   
  59. - (void)dealloc {  
  60.     [field1 release];  
  61.     [field2 release];  
  62.     [field3 release];  
  63.     [field4 release];  
  64.     [super dealloc];  
  65. }  
  66.   
  67. @end  

SQLite3

打开数据库

sqlite3 *database;

int result = sqlite3_open("/path/to/database/file",&database);

如果result等于SQLITE_OK,则表示数据库已经成功打开。SQLite3是采用可以移植的C,所有它不知道什么是NSString,所幸,,有一个NSString方法,该方法从NSString实例生成C字符串

char *cStringPath = [pathString UTF8String];

关闭数据库

sqlite3_close(database)

创建表


查询


结果集


[csharp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. #import "/usr/include/sqlite3.h"  
  3. #define kFilename @"data.sqlite3"  
  4.   
  5. @interface PersistenceViewController : UIViewController  
  6. {  
  7.     IBOutlet UITextField *field1;  
  8.     IBOutlet UITextField *field2;  
  9.     IBOutlet UITextField *field3;  
  10.     IBOutlet UITextField *field4;  
  11.       
  12.     sqlite3 *database;  
  13. }  
  14.   
  15. @property (nonatomic,retain) UITextField *field1;  
  16. @property (nonatomic,retain) UITextField *field2;  
  17. @property (nonatomic,retain) UITextField *field3;  
  18. @property (nonatomic,retain) UITextField *field4;  
  19.   
  20. - (NSString *) dataFilePath;  
  21. - (void) applicationWillTerminate:(NSNotification *) notifacation;  
  22.   
  23. @end  

[csharp]  view plain copy
  1. #import "ViewController.h"  
  2. #import "FourLines.h"  
  3.   
  4. @implementation PersistenceViewController  
  5.   
  6. @synthesize field1;  
  7. @synthesize field2;  
  8. @synthesize field3;  
  9. @synthesize field4;  
  10.   
  11. - (NSString *) dataFilePath  
  12. {  
  13.     NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  14.     NSString *documentsDirectory = [path objectAtIndex:0];  
  15.       
  16.     return [documentsDirectory stringByAppendingPathComponent:kFilename];  
  17. }  
  18.   
  19. - (void) applicationWillTerminate:(NSNotification *) notifacation  
  20. {  
  21.     for (int i=1; i<=4; i++) {  
  22.         NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d",i];  
  23.         UITextField *field = [self valueForKey:fieldName];  
  24.         [fieldName release];  
  25.         NSString *update = [[NSString alloc] initWithFormat:@"insert or replace into fields(row,field_data) values (%d,'%@');",i,field.text];  
  26.         char *errorMsg;  
  27.         if (sqlite3_exec(database, [update UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {  
  28.             NSAssert(0, @"Error updating table : %s", errorMsg);  
  29.             sqlite3_free(errorMsg);  
  30.         }  
  31.     }  
  32.     sqlite3_close(database);  
  33. }  
  34.   
  35. - (void) viewDidLoad  
  36. {  
  37.     if (sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) {  
  38.         sqlite3_close(database);  
  39.         NSAssert(0, @"");  
  40.     }  
  41.       
  42.     char *errorMsg;  
  43.     NSString *createSQL = @"create table if not exist fields (row integer primary key,field_data text);";  
  44.     if (sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg)!= SQLITE_OK) {  
  45.         sqlite3_close(database);  
  46.         NSAssert(0, @"");  
  47.     }  
  48.     NSString *query = @"select row,field_data from fields order by row";  
  49.     sqlite3_stmt *statement;  
  50.     if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {  
  51.         while (sqlite3_step(statement) == SQLITE_OK) {  
  52.             int row = sqlite3_column_int(statement, 0);  
  53.             char *rowData = (char *)sqlite3_column_text(statement, 1);  
  54.             NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d",row];  
  55.             NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];  
  56.             UITextField *field = [self valueForKey:fieldName];  
  57.             field.text = fieldValue;  
  58.             [fieldName release];  
  59.             [fieldValue release];  
  60.         }  
  61.         sqlite3_finalize(statement);  
  62.     }  
  63. }  
  64.   
  65. - (void)dealloc {  
  66.     [field1 release];  
  67.     [field2 release];  
  68.     [field3 release];  
  69.     [field4 release];  
  70.     [super dealloc];  
  71. }  
  72.   
  73. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值