属性列表
对象归档
SQLite3
CoreData
AppSettings
普通文件存储
1、属性列表
-
- #import <UIKit/UIKit.h>
- #define kFilename @"data.plist"
-
- @interface Persistence1ViewController : UIViewController {
- UITextField *filed1;
- UITextField *field2;
- UITextField *field3;
- UITextField *field4;
- }
- @property (nonatomic, retain) IBOutlet UITextField *field1;
- @property (nonatomic, retain) IBOutlet UITextField *field2;
- @property (nonatomic, retain) IBOutlet UITextField *field3;
- @property (nonatomic, retain) IBOutlet UITextField *field4;
-
- - (NSString *)dataFilePath;
- - (void)applicationWillResignActive:(NSNotification *)notification;
-
- @end
===================================================================================
===================================================================================
2、对象归档
-
- #import <Foundation/Foundation.h>
-
- @interface Fourlines : NSObject <NSCoding, NSCopying> {
- NSString *field1;
- NSString *field2;
- NSString *field3;
- NSString *field4;
- }
- @property (nonatomic, retain) NSString *field1;
- @property (nonatomic, retain) NSString *field2;
- @property (nonatomic, retain) NSString *field3;
- @property (nonatomic, retain) NSString *field4;
-
-
- @end
-
- #import "Fourlines.h"
-
- #define kField1Key @"Field1"
- #define kField2Key @"Field2"
- #define kField3Key @"Field3"
- #define kField4Key @"Field4"
-
- @implementation Fourlines
- @synthesize field1;
- @synthesize field2;
- @synthesize field3;
- @synthesize field4;
-
- #pragma mark NSCoding
- - (void)encodeWithCoder:(NSCoder *)aCoder {
- [aCoder encodeObject:field1 forKey:kField1Key];
- [aCoder encodeObject:field2 forKey:kField2Key];
- [aCoder encodeObject:field3 forKey:kField3Key];
- [aCoder encodeObject:field4 forKey:kField4Key];
- }
-
- -(id) initWithCoder:(NSCoder *)aDecoder {
- if(self = [super init]) {
- field1 = [[aDecoder decodeObjectForKey:kField1Key] retain];
- field2 = [[aDecoder decodeObjectForKey:kField2Key] retain];
- field3 = [[aDecoder decodeObjectForKey:kField3Key] retain];
- field4 = [[aDecoder decodeObjectForKey:kField4Key] retain];
- }
- return self;
- }
-
- #pragma mark -
- #pragma mark NSCopying
- - (id) copyWithZone:(NSZone *)zone {
- Fourlines *copy = [[[self class] allocWithZone: zone] init];
- copy.field1 = [[self.field1 copyWithZone: zone] autorelease];
- copy.field2 = [[self.field2 copyWithZone: zone] autorelease];
- copy.field3 = [[self.field3 copyWithZone: zone] autorelease];
- copy.field4 = [[self.field4 copyWithZone: zone] autorelease];
- return copy;
- }
- @end
-
- #import <UIKit/UIKit.h>
- #define kFilename @"archive"
- #define kDataKey @"Data"
-
- @interface Persistence2ViewController : UIViewController {
- UITextField *filed1;
- UITextField *field2;
- UITextField *field3;
- UITextField *field4;
- }
- @property (nonatomic, retain) IBOutlet UITextField *field1;
- @property (nonatomic, retain) IBOutlet UITextField *field2;
- @property (nonatomic, retain) IBOutlet UITextField *field3;
- @property (nonatomic, retain) IBOutlet UITextField *field4;
-
- - (NSString *)dataFilePath;
- - (void)applicationWillResignActive:(NSNotification *)notification;
-
- @end
===================================================================================
===================================================================================
3、SQLite
-
- #import <UIKit/UIKit.h>
- #define kFilename @"data.sqlite3"
-
- @interface Persistence3ViewController : UIViewController {
- UITextField *filed1;
- UITextField *field2;
- UITextField *field3;
- UITextField *field4;
- }
- @property (nonatomic, retain) IBOutlet UITextField *field1;
- @property (nonatomic, retain) IBOutlet UITextField *field2;
- @property (nonatomic, retain) IBOutlet UITextField *field3;
- @property (nonatomic, retain) IBOutlet UITextField *field4;
-
- - (NSString *)dataFilePath;
- - (void)applicationWillResignActive:(NSNotification *)notification;
- @end
-
- #import "Persistence3ViewController.h"
- #import <sqlite3.h>
-
- @implementation Persistence3ViewController
- @synthesize field1;
- @synthesize field2;
- @synthesize field3;
- @synthesize field4;
-
- - (NSString *)dataFilePath {
-
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
-
- NSString *documentsDirectory = [paths objectAtIndex:0];
-
- return [documentsDirectory stringByAppendingPathComponent:kFilename];
- }
-
- - (void)applicationWillResignActive:(NSNotification *)notification {
- sqlite3 *database;
- if(sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) {
- sqlite3_close(database);
- NSAssert(0, @"Failed to open database");
- }
-
- for(int i = 1; i <= 4; i++) {
- NSString *fieldname = [[NSString alloc] initWithFormat:@"field%d", i];
- UITextField *field = [self valueForKey:fieldname];
- [fieldname release];
-
- char *update = "INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA) VALUES (?, ?);";
- sqlite3_stmt *stmt;
-
- if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) {
-
- sqlite3_bind_int(stmt, 1, i);
- sqlite3_bind_text(stmt, 2, [field.text UTF8String], -1, NULL);
- } else {
- NSAssert(0, @"Error:Failed to prepare statemen");
- }
-
- int result = sqlite3_step(stmt);
- if(result != SQLITE_DONE) {
- NSAssert1(0, @"Error updating table: %d", result);
- }
-
- sqlite3_finalize(stmt);
- }
- sqlite3_close(database);
- }
-
-
-
-
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSString *filePath = [self dataFilePath];
-
- if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
-
- sqlite3 *database;
- if(sqlite3_open([filePath UTF8String], &database) != SQLITE_OK) {
- sqlite3_close(database);
- NSAssert(0, @"Failed to open database");
- }
-
-
- char *errorMsg;
- NSString *createSQL =
- @"CREATE TABLE IF NOT EXISTS FIELDS (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT);";
- if(sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
- sqlite3_close(database);
- NSAssert(0, @"Error creating table: %s", errorMsg);
- }
-
-
- NSString *query = @"SELECT ROW, FIELD_DATA FROM FIELDS ORDER BY ROW";
- sqlite3_stmt *statement;
-
- if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
- while (sqlite3_step(statement) == SQLITE_ROW) {
- int row = sqlite3_column_int(statement, 0);
- char *rowData = (char *)sqlite3_column_text(statement, 1);
-
- NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d", row];
- NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];
-
- UITextField *field = [self valueForKey:fieldName];
- field.text = fieldValue;
-
- [fieldName release];
- [fieldValue release];
- }
-
- sqlite3_finalize(statement);
- }
- sqlite3_close(database);
- }
-
-
- UIApplication *app = [UIApplication sharedApplication];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationWillResignActive:)
- name:UIApplicationWillResignActiveNotification
- object:app];
- [super viewDidLoad];
- }
-
-
-
- - (void)didReceiveMemoryWarning {
-
- [super didReceiveMemoryWarning];
-
-
- }
-
- - (void)viewDidUnload {
- self.field1 = nil;
- self.field2 = nil;
- self.field3 = nil;
- self.field4 = nil;
- [super viewDidUnload];
- }
-
-
- - (void)dealloc {
- [field1 release];
- [field2 release];
- [field3 release];
- [field4 release];
- [super dealloc];
- }
-
- @end
===================================================================================
===================================================================================
4、Core Data
-
- #import <UIKit/UIKit.h>
-
-
- @interface PersistenceViewController : UIViewController {
- UITextField *filed1;
- UITextField *field2;
- UITextField *field3;
- UITextField *field4;
- }
- @property (nonatomic, retain) IBOutlet UITextField *field1;
- @property (nonatomic, retain) IBOutlet UITextField *field2;
- @property (nonatomic, retain) IBOutlet UITextField *field3;
- @property (nonatomic, retain) IBOutlet UITextField *field4;
-
- @end
5、AppSettings
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
===================================================================================
===================================================================================
6、普通文件存储
这种方式即自己将数据通过IO保存到文件,或从文件读取。
声明:此篇所有代码来自《Iphone4与IPad开发基础教程》