IOS 数据存储

ios数据存储包括以下几种存储机制:

属性列表

对象归档

SQLite3

CoreData

AppSettings

普通文件存储

1、属性列表

  1. //
  2. //  Persistence1ViewController.h
  3. //  Persistence1
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #define kFilename @"data.plist"
  10. @interface Persistence1ViewController : UIViewController {
  11. UITextField *filed1;
  12. UITextField *field2;
  13. UITextField *field3;
  14. UITextField *field4;
  15. }
  16. @property (nonatomic, retain) IBOutlet UITextField *field1;
  17. @property (nonatomic, retain) IBOutlet UITextField *field2;
  18. @property (nonatomic, retain) IBOutlet UITextField *field3;
  19. @property (nonatomic, retain) IBOutlet UITextField *field4;
  20. - (NSString *)dataFilePath;
  21. - (void)applicationWillResignActive:(NSNotification *)notification;
  22. @end
  1. //
  2. //  Persistence1ViewController.m
  3. //  Persistence1
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "Persistence1ViewController.h"
  9. @implementation Persistence1ViewController
  10. @synthesize field1;
  11. @synthesize field2;
  12. @synthesize field3;
  13. @synthesize field4;
  14. //数据文件的完整路径
  15. - (NSString *)dataFilePath {
  16. //检索Documents目录路径。第二个参数表示将搜索限制在我们的应用程序沙盒中
  17. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  18. //每个应用程序只有一个Documents目录
  19. NSString *documentsDirectory = [paths objectAtIndex:0];
  20. //创建文件名
  21. return [documentsDirectory stringByAppendingPathComponent:kFilename];
  22. }
  23. //应用程序退出时,将数据保存到属性列表文件
  24. - (void)applicationWillResignActive:(NSNotification *)notification {
  25. NSMutableArray *array = [[NSMutableArray alloc] init];
  26. [array addObject: field1.text];
  27. [array addObject: field2.text];
  28. [array addObject: field3.text];
  29. [array addObject: field4.text];
  30. [array writeToFile:[self dataFilePath] atomically:YES];
  31. [array release];
  32. }
  33. /*
  34. // The designated initializer. Override to perform setup that is required before the view is loaded.
  35. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  36. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  37. if (self) {
  38. // Custom initialization
  39. }
  40. return self;
  41. }
  42. */
  43. /*
  44. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  45. - (void)loadView {
  46. }
  47. */
  48. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. NSString *filePath = [self dataFilePath];
  52. //检查数据文件是否存在
  53. if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  54. NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
  55. field1.text = [array objectAtIndex:0];
  56. field2.text = [array objectAtIndex:1];
  57. field3.text = [array objectAtIndex:2];
  58. field4.text = [array objectAtIndex:3];
  59. [array release];
  60. }
  61. UIApplication *app = [UIApplication sharedApplication];
  62. [[NSNotificationCenter defaultCenter] addObserver:self
  63. selector:@selector(applicationWillResignActive:)
  64. name:UIApplicationWillResignActiveNotification
  65. object:app];
  66. [super viewDidLoad];
  67. }
  68. /*
  69. // Override to allow orientations other than the default portrait orientation.
  70. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  71. // Return YES for supported orientations
  72. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  73. }
  74. */
  75. - (void)didReceiveMemoryWarning {
  76. // Releases the view if it doesn't have a superview.
  77. [super didReceiveMemoryWarning];
  78. // Release any cached data, images, etc that aren't in use.
  79. }
  80. - (void)viewDidUnload {
  81. self.field1 = nil;
  82. self.field2 = nil;
  83. self.field3 = nil;
  84. self.field4 = nil;
  85. [super viewDidUnload];
  86. }
  87. - (void)dealloc {
  88. [field1 release];
  89. [field2 release];
  90. [field3 release];
  91. [field4 release];
  92. [super dealloc];
  93. }
  94. @end

===================================================================================

===================================================================================

2、对象归档

  1. //
  2. //  Fourlines.h
  3. //  Persistence2
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface Fourlines : NSObject <NSCoding, NSCopying> {
  10. NSString *field1;
  11. NSString *field2;
  12. NSString *field3;
  13. NSString *field4;
  14. }
  15. @property (nonatomic, retain) NSString *field1;
  16. @property (nonatomic, retain) NSString *field2;
  17. @property (nonatomic, retain) NSString *field3;
  18. @property (nonatomic, retain) NSString *field4;
  19. @end
  1. //
  2. //  Fourlines.m
  3. //  Persistence2
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "Fourlines.h"
  9. #define kField1Key @"Field1"
  10. #define kField2Key @"Field2"
  11. #define kField3Key @"Field3"
  12. #define kField4Key @"Field4"
  13. @implementation Fourlines
  14. @synthesize field1;
  15. @synthesize field2;
  16. @synthesize field3;
  17. @synthesize field4;
  18. #pragma mark NSCoding
  19. - (void)encodeWithCoder:(NSCoder *)aCoder {
  20. [aCoder encodeObject:field1 forKey:kField1Key];
  21. [aCoder encodeObject:field2 forKey:kField2Key];
  22. [aCoder encodeObject:field3 forKey:kField3Key];
  23. [aCoder encodeObject:field4 forKey:kField4Key];
  24. }
  25. -(id) initWithCoder:(NSCoder *)aDecoder {
  26. if(self = [super init]) {
  27. field1 = [[aDecoder decodeObjectForKey:kField1Key] retain];
  28. field2 = [[aDecoder decodeObjectForKey:kField2Key] retain];
  29. field3 = [[aDecoder decodeObjectForKey:kField3Key] retain];
  30. field4 = [[aDecoder decodeObjectForKey:kField4Key] retain];
  31. }
  32. return self;
  33. }
  34. #pragma mark -
  35. #pragma mark NSCopying
  36. - (id) copyWithZone:(NSZone *)zone {
  37. Fourlines *copy = [[[self class] allocWithZone: zone] init];
  38. copy.field1 = [[self.field1 copyWithZone: zone] autorelease];
  39. copy.field2 = [[self.field2 copyWithZone: zone] autorelease];
  40. copy.field3 = [[self.field3 copyWithZone: zone] autorelease];
  41. copy.field4 = [[self.field4 copyWithZone: zone] autorelease];
  42. return copy;
  43. }
  44. @end
  1. //
  2. //  Persistence2ViewController.h
  3. //  Persistence2
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #define kFilename @"archive"
  10. #define kDataKey @"Data"
  11. @interface Persistence2ViewController : UIViewController {
  12. UITextField *filed1;
  13. UITextField *field2;
  14. UITextField *field3;
  15. UITextField *field4;
  16. }
  17. @property (nonatomic, retain) IBOutlet UITextField *field1;
  18. @property (nonatomic, retain) IBOutlet UITextField *field2;
  19. @property (nonatomic, retain) IBOutlet UITextField *field3;
  20. @property (nonatomic, retain) IBOutlet UITextField *field4;
  21. - (NSString *)dataFilePath;
  22. - (void)applicationWillResignActive:(NSNotification *)notification;
  23. @end
  1. //
  2. //  Persistence2ViewController.m
  3. //  Persistence2
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "Persistence2ViewController.h"
  9. #import "Fourlines.h"
  10. @implementation Persistence2ViewController
  11. @synthesize field1;
  12. @synthesize field2;
  13. @synthesize field3;
  14. @synthesize field4;
  15. //数据文件的完整路径
  16. - (NSString *)dataFilePath {
  17. //检索Documents目录路径。第二个参数表示将搜索限制在我们的应用程序沙盒中
  18. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  19. //每个应用程序只有一个Documents目录
  20. NSString *documentsDirectory = [paths objectAtIndex:0];
  21. //创建文件名
  22. return [documentsDirectory stringByAppendingPathComponent:kFilename];
  23. }
  24. //应用程序退出时,将数据保存到属性列表文件
  25. - (void)applicationWillResignActive:(NSNotification *)notification {
  26. Fourlines *fourlines = [[Fourlines alloc] init];
  27. fourlines.field1 = field1.text;
  28. fourlines.field2 = field2.text;
  29. fourlines.field3 = field3.text;
  30. fourlines.field4 = field4.text;
  31. NSMutableData *data = [[NSMutableData alloc] init];//用于存储编码的数据
  32. NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
  33. [archiver encodeObject:fourlines forKey:kDataKey];
  34. [archiver finishEncoding];
  35. [data writeToFile:[self dataFilePath] atomically:YES];
  36. [fourlines release];
  37. [archiver release];
  38. [data release];
  39. }
  40. /*
  41. // The designated initializer. Override to perform setup that is required before the view is loaded.
  42. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  43. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  44. if (self) {
  45. // Custom initialization
  46. }
  47. return self;
  48. }
  49. */
  50. /*
  51. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  52. - (void)loadView {
  53. }
  54. */
  55. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  56. - (void)viewDidLoad {
  57. [super viewDidLoad];
  58. NSString *filePath = [self dataFilePath];
  59. //检查数据文件是否存在
  60. if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  61. //从文件获取用于解码的数据
  62. NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
  63. NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  64. Fourlines *fourlines = [unarchiver decodeObjectForKey:kDataKey];
  65. [unarchiver finishDecoding];
  66. field1.text = fourlines.field1;
  67. field2.text = fourlines.field2;
  68. field3.text = fourlines.field3;
  69. field4.text = fourlines.field4;
  70. [unarchiver release];
  71. [data release];
  72. }
  73. UIApplication *app = [UIApplication sharedApplication];
  74. [[NSNotificationCenter defaultCenter] addObserver:self
  75. selector:@selector(applicationWillResignActive:)
  76. name:UIApplicationWillResignActiveNotification
  77. object:app];
  78. [super viewDidLoad];
  79. }
  80. /*
  81. // Override to allow orientations other than the default portrait orientation.
  82. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  83. // Return YES for supported orientations
  84. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  85. }
  86. */
  87. - (void)didReceiveMemoryWarning {
  88. // Releases the view if it doesn't have a superview.
  89. [super didReceiveMemoryWarning];
  90. // Release any cached data, images, etc that aren't in use.
  91. }
  92. - (void)viewDidUnload {
  93. self.field1 = nil;
  94. self.field2 = nil;
  95. self.field3 = nil;
  96. self.field4 = nil;
  97. [super viewDidUnload];
  98. }
  99. - (void)dealloc {
  100. [field1 release];
  101. [field2 release];
  102. [field3 release];
  103. [field4 release];
  104. [super dealloc];
  105. }
  106. @end

===================================================================================

===================================================================================

3、SQLite

  1. //
  2. //  Persistence3ViewController.h
  3. //  Persistence3
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #define kFilename @"data.sqlite3"
  10. @interface Persistence3ViewController : UIViewController {
  11. UITextField *filed1;
  12. UITextField *field2;
  13. UITextField *field3;
  14. UITextField *field4;
  15. }
  16. @property (nonatomic, retain) IBOutlet UITextField *field1;
  17. @property (nonatomic, retain) IBOutlet UITextField *field2;
  18. @property (nonatomic, retain) IBOutlet UITextField *field3;
  19. @property (nonatomic, retain) IBOutlet UITextField *field4;
  20. - (NSString *)dataFilePath;
  21. - (void)applicationWillResignActive:(NSNotification *)notification;
  22. @end
  1. //
  2. //  Persistence3ViewController.m
  3. //  Persistence3
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "Persistence3ViewController.h"
  9. #import <sqlite3.h>
  10. @implementation Persistence3ViewController
  11. @synthesize field1;
  12. @synthesize field2;
  13. @synthesize field3;
  14. @synthesize field4;
  15. //数据文件的完整路径
  16. - (NSString *)dataFilePath {
  17. //检索Documents目录路径。第二个参数表示将搜索限制在我们的应用程序沙盒中
  18. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  19. //每个应用程序只有一个Documents目录
  20. NSString *documentsDirectory = [paths objectAtIndex:0];
  21. //创建文件名
  22. return [documentsDirectory stringByAppendingPathComponent:kFilename];
  23. }
  24. //应用程序退出时,将数据保存到属性列表文件
  25. - (void)applicationWillResignActive:(NSNotification *)notification {
  26. sqlite3 *database;
  27. if(sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK) {
  28. sqlite3_close(database);
  29. NSAssert(0, @"Failed to open database");
  30. }
  31. for(int i = 1; i <= 4; i++) {
  32. NSString *fieldname = [[NSString alloc] initWithFormat:@"field%d", i];
  33. UITextField *field = [self valueForKey:fieldname];
  34. [fieldname release];
  35. char *update = "INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA) VALUES (?, ?);";
  36. sqlite3_stmt *stmt;
  37. //将SQL语句编译为sqlite内部一个结构体(sqlite3_stmt),类似java JDBC的PreparedStatement预编译
  38. if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) {
  39. //在bind参数的时候,参数列表的index从1开始,而取出数据的时候,列的index是从0开始
  40. sqlite3_bind_int(stmt, 1, i);
  41. sqlite3_bind_text(stmt, 2, [field.text UTF8String], -1, NULL);
  42. } else {
  43. NSAssert(0, @"Error:Failed to prepare statemen");
  44. }
  45. //执行SQL文,获取结果
  46. int result = sqlite3_step(stmt);
  47. if(result != SQLITE_DONE) {
  48. NSAssert1(0, @"Error updating table: %d", result);
  49. }
  50. //释放stmt占用的内存(sqlite3_prepare_v2()分配的)
  51. sqlite3_finalize(stmt);
  52. }
  53. sqlite3_close(database);
  54. }
  55. /*
  56. // The designated initializer. Override to perform setup that is required before the view is loaded.
  57. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  58. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  59. if (self) {
  60. // Custom initialization
  61. }
  62. return self;
  63. }
  64. */
  65. /*
  66. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  67. - (void)loadView {
  68. }
  69. */
  70. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  71. - (void)viewDidLoad {
  72. [super viewDidLoad];
  73. NSString *filePath = [self dataFilePath];
  74. //检查数据文件是否存在
  75. if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  76. //打开数据库
  77. sqlite3 *database;
  78. if(sqlite3_open([filePath UTF8String], &database) != SQLITE_OK) {
  79. sqlite3_close(database);
  80. NSAssert(0, @"Failed to open database");
  81. }
  82. //创建表
  83. char *errorMsg;
  84. NSString *createSQL =
  85. @"CREATE TABLE IF NOT EXISTS FIELDS (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT);";
  86. if(sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
  87. sqlite3_close(database);
  88. NSAssert(0, @"Error creating table: %s", errorMsg);
  89. }
  90. //查询
  91. NSString *query = @"SELECT ROW, FIELD_DATA FROM FIELDS ORDER BY ROW";
  92. sqlite3_stmt *statement;
  93. //设置nByte可以加速操作
  94. if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
  95. while (sqlite3_step(statement) == SQLITE_ROW) {//返回每一行
  96. int row = sqlite3_column_int(statement, 0);
  97. char *rowData = (char *)sqlite3_column_text(statement, 1);
  98. NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d", row];
  99. NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];
  100. UITextField *field = [self valueForKey:fieldName];
  101. field.text = fieldValue;
  102. [fieldName release];
  103. [fieldValue release];
  104. }
  105. //释放statement占用的内存(sqlite3_prepare()分配的)
  106. sqlite3_finalize(statement);
  107. }
  108. sqlite3_close(database);
  109. }
  110. UIApplication *app = [UIApplication sharedApplication];
  111. [[NSNotificationCenter defaultCenter] addObserver:self
  112. selector:@selector(applicationWillResignActive:)
  113. name:UIApplicationWillResignActiveNotification
  114. object:app];
  115. [super viewDidLoad];
  116. }
  117. /*
  118. // Override to allow orientations other than the default portrait orientation.
  119. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  120. // Return YES for supported orientations
  121. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  122. }
  123. */
  124. - (void)didReceiveMemoryWarning {
  125. // Releases the view if it doesn't have a superview.
  126. [super didReceiveMemoryWarning];
  127. // Release any cached data, images, etc that aren't in use.
  128. }
  129. - (void)viewDidUnload {
  130. self.field1 = nil;
  131. self.field2 = nil;
  132. self.field3 = nil;
  133. self.field4 = nil;
  134. [super viewDidUnload];
  135. }
  136. - (void)dealloc {
  137. [field1 release];
  138. [field2 release];
  139. [field3 release];
  140. [field4 release];
  141. [super dealloc];
  142. }
  143. @end

===================================================================================

===================================================================================

4、Core Data

  1. //
  2. //  PersistenceViewController.h
  3. //  Persistence4
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. @interface PersistenceViewController : UIViewController {
  10. UITextField *filed1;
  11. UITextField *field2;
  12. UITextField *field3;
  13. UITextField *field4;
  14. }
  15. @property (nonatomic, retain) IBOutlet UITextField *field1;
  16. @property (nonatomic, retain) IBOutlet UITextField *field2;
  17. @property (nonatomic, retain) IBOutlet UITextField *field3;
  18. @property (nonatomic, retain) IBOutlet UITextField *field4;
  19. @end
  1. //
  2. //  PersistenceViewController.m
  3. //  Persistence4
  4. //
  5. //  Created by liu lavy on 11-10-3.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "PersistenceViewController.h"
  9. #import "Persistence4AppDelegate.h"
  10. @implementation PersistenceViewController
  11. @synthesize field1;
  12. @synthesize field2;
  13. @synthesize field3;
  14. @synthesize field4;
  15. -(void) applicationWillResignActive:(NSNotification *)notification {
  16. Persistence4AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  17. NSManagedObjectContext *context = [appDelegate managedObjectContext];
  18. NSError *error;
  19. for(int i = 1; i <= 4; i++) {
  20. NSString *fieldName = [NSString stringWithFormat:@"field%d", i];
  21. UITextField *theField = [self valueForKey:fieldName];
  22. //创建提取请求
  23. NSFetchRequest *request = [[NSFetchRequest alloc] init];
  24. //创建实体描述并关联到请求
  25. NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line"
  26. inManagedObjectContext:context];
  27. [request setEntity:entityDescription];
  28. //设置检索数据的条件
  29. NSPredicate *pred = [NSPredicate predicateWithFormat:@"(lineNum = %d)", i];
  30. [request setPredicate:pred];
  31. NSManagedObject *theLine = nil;
  32. ////检查是否返回了标准匹配的对象,如果有则加载它,如果没有则创建一个新的托管对象来保存此字段的文本
  33. NSArray *objects = [context executeFetchRequest:request error:&error];
  34. if(!objects) {
  35. NSLog(@"There was an error");
  36. }
  37. //if(objects.count > 0) {
  38. //  theLine = [objects objectAtIndex:0];
  39. //} else {
  40. //创建一个新的托管对象来保存此字段的文本
  41. theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line"
  42. inManagedObjectContext:context];
  43. [theLine setValue:[NSNumber numberWithInt:i] forKey:@"lineNum"];
  44. [theLine setValue:theField.text forKey:@"lineText"];
  45. //}
  46. [request release];
  47. }
  48. //通知上下文保存更改
  49. [context save:&error];
  50. }
  51. // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
  52. /*
  53. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  54. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  55. if (self) {
  56. // Custom initialization.
  57. }
  58. return self;
  59. }
  60. */
  61. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  62. - (void)viewDidLoad {
  63. Persistence4AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  64. NSManagedObjectContext *context = [appDelegate managedObjectContext];
  65. //创建一个实体描述
  66. NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
  67. //创建一个请求,用于提取对象
  68. NSFetchRequest *request = [[NSFetchRequest alloc] init];
  69. [request setEntity:entityDescription];
  70. //检索对象
  71. NSError *error;
  72. NSArray *objects = [context executeFetchRequest:request error:&error];
  73. if(!objects) {
  74. NSLog(@"There was an error!");
  75. }
  76. for(NSManagedObject *obj in objects) {
  77. NSNumber *lineNum = [obj valueForKey:@"lineNum"];
  78. NSString *lineText = [obj valueForKey:@"lineText"];
  79. NSString *fieldName = [NSString stringWithFormat:@"field%d", [lineNum integerValue]];
  80. UITextField *textField = [self valueForKey:fieldName];
  81. textField.text = lineText;
  82. }
  83. [request release];
  84. UIApplication *app = [UIApplication sharedApplication];
  85. [[NSNotificationCenter defaultCenter] addObserver:self
  86. selector:@selector(applicationWillResignActive:)
  87. name:UIApplicationWillResignActiveNotification
  88. object:app];
  89. [super viewDidLoad];
  90. }
  91. /*
  92. // Override to allow orientations other than the default portrait orientation.
  93. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  94. // Return YES for supported orientations.
  95. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  96. }
  97. */
  98. - (void)didReceiveMemoryWarning {
  99. // Releases the view if it doesn't have a superview.
  100. [super didReceiveMemoryWarning];
  101. // Release any cached data, images, etc. that aren't in use.
  102. }
  103. - (void)viewDidUnload {
  104. self.field1 = nil;
  105. self.field2 = nil;
  106. self.field3 = nil;
  107. self.field4 = nil;
  108. [super viewDidUnload];
  109. }
  110. - (void)dealloc {
  111. [field1 release];
  112. [field2 release];
  113. [field3 release];
  114. [field4 release];
  115. [super dealloc];
  116. }
  117. @end

5、AppSettings

  1. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

===================================================================================

===================================================================================

6、普通文件存储

这种方式即自己将数据通过IO保存到文件,或从文件读取。

声明:此篇所有代码来自《Iphone4与IPad开发基础教程》

最新文章

  1. blog (后续更新)
  2. solr安装笔记与定时器任务
  3. RCP:如何把Preferences中的项从一个类别移动到另一个类别
  4. 深入PHP内核之ZVAL
  5. 年前辞职-WCF入门学习(3)
  6. JS - 超强大文本动画插件Textillate.js
  7. 如何将character_set_database latin1 改为 gbk(转)
  8. 快速创建maven 工程:simple java工程,webapp
  9. [转][IIS]发布网站,提示用户 &#39;IIS APPPOOL\***&#39; 登录失败。
  10. 1)phpmyadmin导入数据库大小限制修改
  11. js中getByClass()函数
  12. vue的一些随记
  13. MariaDB:删除数据库报错:error: 'Error dropping database (can't rmdir './shiro', errno: 39)'
  14. LeetCode OJ 79. Word Search
  15. Homebrew替换源
  16. dotnet core在Task中使用依赖注入的Service/EFContext
  17. angular 首屏优化
  18. POJ1258 (最小生成树prim)
  19. 使用强大的可视化工具redislive来监控我们的redis
  20. webservice原理及基于cxf开发的基本流程

热门文章

  1. JDBC | 第七章: JDBC数据库连接池使用
  2. 【MarkDown】github readme添加图片 Markdown语法添加图片,适用各种markdown语法
  3. 浅析一个lua文件窥slua工作机制
  4. 和同事谈谈Flood Fill 算法
  5. Linux平台Zabbix Agent的安装配置
  6. 判断同名股票是否存在的MyBatis查询函数写法
  7. 在Nginx里指定ip_hash的方式解决Tomcat集群session的问题
  8. mysql数据库常见问题修改(待补充)
  9. 第2课 - 初识makefile的结构
  10. (专题四)05 matlab视角处理