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

相关内容