歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS 數據存儲

iOS 數據存儲

日期:2017/3/1 11:11:06   编辑:Linux編程

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
Copyright © Linux教程網 All Rights Reserved