iOS上常用四种数据存取方法

来源:互联网 发布:注册域名免费 编辑:程序博客网 时间:2024/06/17 15:22

iOS上常用四种数据存取方法有:

属性列表

对象归档

iOS的嵌入式关系数据库(SQLite3)

苹果公司提供持久性共聚CoreData


它们的应用程序都有自己的/Documents文件夹,各自的应用程序只能读写自己的/Documents目录内容


1.创建一个新工程叫PersistenceDemo;File->New->Project->single View Application ->next


2.在PersistenceViewController.h文件声明输出口和和实例方法

 

[cpp] viewplaincopy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface PersistenceViewControlleUIViewController  
  4.   
  5. @property (weak, nonatomic) IBOutlet UITextField *field1;  
  6. @property (weak, nonatomic) IBOutlet UITextField *field2;  
  7. @property (weak, nonatomic) IBOutlet UITextField *field3;  
  8. @property (weak, nonatomic) IBOutlet UITextField *field4;  
  9.   
  10. -(NSString *)dataFilePath;  
  11. -(void)applicationWillResignActive:(NSNotification *)notification;  
  12.   
  13. @end  

3.打开PersistenceViewController.xib文件拖4个laibel静态标签和4个TextField,然后和输出口关联(右键Fiel‘sOwner 拖到TextField上松开)

 

 

 

4.在对应的.m文件中

 

 

dataFilePath将kFileName串联到Documents目录的路径,以创建并返回数据文件的完整路径名

 

[cpp] viewplaincopy
  1. -(NSString *)dataFilePath  
  2.  
  3.       
  4.       
  5.     NSArray *paths NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  6. //  数组索引0处Documentd目录,    
  7.     NSString *documentDirectory [paths objectAtIndex:0];  
  8. //    返回一个kFileName的完整路径  
  9.     return [documentDirectory stringByAppendingPathComponent:kFileName];  
  10.  

 


[cpp] viewplaincopy
  1. (void)viewDidLoad  
  2.  
  3.     [super viewDidLoad];  
  4. //  检查数据文件是否存在,不存在则不加载    
  5.     NSString *filePath [self dataFilePath];  
  6.     if ([[NSFileManager defaultManager]fileExistsAtPath:filePath])  
  7.         NSArray *array [[NSArray alloc] initWithContentsOfFile:filePath];  
  8.           
  9.         field1.text [array objectAtIndex:0];  
  10.         field2.text [array objectAtIndex:1];  
  11.         field3.text [array objectAtIndex:2];  
  12.         field4.text [array objectAtIndex:3];  
  13.      
  14. //    注册一个通知,按下home键,执行applicationWillResignActive:方法  
  15.     UIApplication *app [UIApplication sharedApplication];  
  16.     [[NSNotificationCenter defaultCenter]addObserver:self  
  17.                                             selector:@selector(applicationWillResignActive:)  
  18.                                                 name:UIApplicationWillTerminateNotification  
  19.                                               object:app];  
  20.       
  21.       
  22.  


所有通知都接受一个NSNotification的参数
[cpp] viewplaincopy
  1. //当按下home键的时候调用这个方法  
  2. -(void)applicationWillResignActive:(NSNotification *)notification  
  3.  
  4.     NSMutableArray *array [[NSMutableArray alloc] init];  
  5.     [array addObject:field1.text];  
  6.     [array addObject:field2.text];  
  7.     [array addObject:field3.text];  
  8.     [array addObject:field4.text];  
  9. //    将TextField中数据写入到属性表之中  
  10.     [array writeToFile:[self dataFilePath] atomically:YES];  
  11.  

5.NSNotification和 NSNotificationCenter

Notification对象非常简单.它就是poster要提供给observer的信息包裹. notification对象有两个重要的成员变量: name 和object.一般object都是指向poster(为了让observer在接受到notification时可以回调到poster)
所以,notification有两个方法
    -(NSString *)name
    -(id)object
NSNotificaitonCernter是架构的大脑了.它允许我们注册observer对象,发送notification, 撤销observer对象注册
下面是它的一些常用方法
+(NSNotificationCenter *)defaultCenter
返回notificationcenter [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]

-(void)addObserver:(id)anObserver
          selector:(SEL)aSelector
              name:(NSString *)notificationName
            object:(id)anObject

注册anObserver对象:接受名字为notificationName,发送者为anObject的notification.当anObject发送名字为notificationName的notification时,将会调用anObserver的aSelector方法,参数为该notification.如果notificationName为nil. 那么notificationcenter将anObject发送的所有notification转发给observer
.如果anObject为nil.那么notificationcenter将所有名字为notificationName的notification转发给observer

-(void)postNotification:(NSNotification*)notification
发送notification至notificationcenter
-(void)postNotificationName:(NSString *)aName
                      object:(id)anObject

创建并发送一个notification
-(void)removeObserver:(id)observer
移除observer

原创粉丝点击