iOS上常用四种数据存取方法
来源:互联网 发布:注册域名免费 编辑:程序博客网 时间:2024/06/17 15:22
iOS上常用四种数据存取方法有:
属性列表
对象归档
iOS的嵌入式关系数据库(SQLite3)
苹果公司提供持久性共聚CoreData
它们的应用程序都有自己的/Documents文件夹,各自的应用程序只能读写自己的/Documents目录内容
1.创建一个新工程叫PersistenceDemo;File->New->Project->single View Application ->next
2.在PersistenceViewControlle
- #import
<UIKit/UIKit.h> -
- @interface
PersistenceViewControlle r : UIViewController -
- @property
(weak, nonatomic) IBOutlet UITextField *field1; - @property
(weak, nonatomic) IBOutlet UITextField *field2; - @property
(weak, nonatomic) IBOutlet UITextField *field3; - @property
(weak, nonatomic) IBOutlet UITextField *field4; -
- -(NSString
*)dataFilePath; - -(void)applicationWillResignAct
ive:(NSNotification *)notification; -
- @end
3.打开PersistenceViewControlle
4.在对应的.m文件中
dataFilePath将kFileName串联到Documents目录的路径,以创建并返回数据文件的完整路径名
- -(NSString
*)dataFilePath - {
-
-
-
NSArray *paths = NSSearchPathForDirectori esInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - //
数组索引0处Documentd目录, -
NSString *documentDirectory = [paths objectAtIndex:0]; - //
返回一个kFileName的完整路径 -
return [documentDirectory stringByAppendingPathCom ponent:kFileName]; - }
- -
( void)viewDidLoad - {
-
[super viewDidLoad]; - //
检查数据文件是否存在,不存在则不加载 -
NSString *filePath = [self dataFilePath]; -
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) { -
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; -
-
field1.text = [array objectAtIndex:0]; -
field2.text = [array objectAtIndex:1]; -
field3.text = [array objectAtIndex:2]; -
field4.text = [array objectAtIndex:3]; -
} - //
注册一个通知,按下home键,执行applicationWillResignAct ive:方法 -
UIApplication *app = [UIApplication sharedApplication]; -
[[NSNotificationCenter defaultCenter]addObserver:self -
selector:@selector(applicationWillResignAct ive:) -
name:UIApplicationWillTermina teNotification -
object:app]; -
-
- }
- //当按下home键的时候调用这个方法
- -(void)applicationWillResignAct
ive:(NSNotification *)notification - {
-
NSMutableArray *array = [[NSMutableArray alloc] init]; -
[array addObject:field1.text]; -
[array addObject:field2.text]; -
[array addObject:field3.text]; -
[array addObject:field4.text]; - //
将TextField中数据写入到属性表之中 -
[array writeToFile:[self dataFilePath] atomically:YES]; - }
5.NSNotification和 NSNotificationCenter
Notification对象非常简单.它就是poster要提供给observer的信息包裹. notification对象有两个重要的成员变量: name 和object.一般object都是指向poster(为了让observer在接受到notification时可以回调到poster)
所以,notification有两个方法
NSNotificaitonCernter是架构的大脑了.它允许我们注册observer对象,发送notification, 撤销observer对象注册
下面是它的一些常用方法
+(NSNotificationCenter *)defaultCenter
返回notificationcenter [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]
-(void)addObserver:(id)anObserver
注册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
创建并发送一个notification
-(void)removeObserver:(id)observer
移除observer
- iOS上常用四种数据存取方法
- 在IOS的数据处理中常用的四种数据存取方式
- iOS中常用的四种数据持久化方法
- iOS中常用的四种数据持久化方法
- IOS中常用的四种数据持久化方法
- IOS中常用的四种数据持久化方法
- iOS中常用的四种数据持久化方法
- IOS-常用的四种数据持久化方法
- iOS 常用四种数据存储方式
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- IOS中常用的四种数据持久化方法简介
- File Save type 归档形式存储文件…
- QuartzCore/QuartzCore框架
- iOS项目常用第三方开源库
- UITabBarController使用详解
- iOS单例模式的实现
- iOS上常用四种数据存取方法
- 在ios中设置UINavigationBar的背景…
- iOS中侧边栏的添加
- iOS中NSNotificationCenter…
- 关于 下拉刷新EGORefre…
- 我已率先抢占昵称,我的昵称独一无…
- UIView的常用操作 详细说明
- CGContextRef用法
- cocos2d-x中CCArray使用时注…