保存数据-1应用程序得沙盒

1//获取沙盒目录(Documents)

    /*NSDocumentDirectory表明我们正在查找Documents目录得路径

      NSUserDomainMask表明我们将搜索限制在我们应用程序得沙盒中 希望该函数查看用户的主目录

      每个应用程序只有一个documents目录 即位于数组中索引值为0处的目录

     */

    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    /*

     获得每个程序自己的documents目录

     */

    NSString *filePath = [paths objectAtIndex:0];

    /*

     在刚刚检索到的路径后边添加一个字符串 来创建一个文件名 如下

     */

    NSString *path = [filePath stringByAppendingPathComponent:dbName];

    /*

     调用之后 path将包含在theText.txt文件的完整路径 该文件将会 位于应用程序的Documents目录 之后可以使用path来创建、读取和写入文件

     */

2

//获取临时目录(tmp

    /*

      调用下边的函数将会返回一个字符串 该字符串包含在应用程序临时的目录的完整路径

     */

    NSString *tempPath = NSTemporaryDirectory();

    /*

     为将要存储在临时文件下的文件创建一个文件名 在该目录中创建一个到该文件的路径

     */

    NSString *tempFile  =[tempPath stringByAppendingPathComponent:@"tempFile.txt"];

3 数据的持久化

(1)属性列表序列化

序列化对象是指将对象转化为字节流 以便存储到文件或者通过网络传输, 将某些对象放在集合类中 然后使用集合类的writeToFile:atcmically将它们存贮在属性列表可以按照该方法序列化的oc类

NSArray;NSMutableArray;NSDictionary;NSMutableDictionary;NSData;

    NSMutableData;NSString;NSMutableString;NSNumber;NSdate

缺点:无法将自定义的对象序列化到属性列表中 也无法直接使用NSURL,UIImage,UIColor

优点:将静态数据包含在程序中的最佳方法 例如在应用程序包含一个选择器的时候,将项目列表包含到选取器中的最佳方法 创建一个属性列表文件 将其放在项目的Resource文件夹中 这会将其编译到应用程序中

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@property (retain, nonatomic) IBOutlet UITextField *field1;


@property (retain, nonatomic) IBOutlet UITextField *field2;


@property (retain, nonatomic) IBOutlet UITextField *field3;


@property (retain, nonatomic) IBOutlet UITextField *field4;


- (NSString *)dataFilePath;


@end

#import "ViewController.h"


#define kFilename @"data.plist"


@interface ViewController ()


@end


@implementation ViewController

- (void)dealloc

{

    [_field1 release];

    [_field2 release];

    [_field3 release];

    [_field4 release];

    [super dealloc];

}

/*

 将文件名串联到documents 目录的路径

 */

- (NSString *)dataFilePath

{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentDirectory = [paths objectAtIndex:0];

    return [documentDirectory stringByAppendingPathComponent:kFilename];

    

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    NSString *filePath = [self dataFilePath];

    NSLog(@"filePath=%@",filePath);

    /*

     检查数据文件是不是存在 如果不存在 不希望加载 

     如果存在 用文件的内容实例化数组 将数组中的内容赋值到textFiled

    */

    if ([[NSFileManager defaultManager]fileExistsAtPath:filePath])

    {

        NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];

        self.field1.text = [array objectAtIndex:0];

        self.field2.text = [array objectAtIndex:1];

        self.field3.text = [array objectAtIndex:2];

        self.field4.text = [array objectAtIndex:3];

    }

    UIApplication *app = [UIApplication sharedApplication];

    /*

     第一个参数是观察者参数 self 意味着viewCotroller是需要通知的对象 

     第二个参数 将一个选择器传递给 刚才编写的applicationWillResignActive:方法

     告知通知中心在发布该通知时调用这个方法 

     第三个参数是 希望接收通知的名称 

     第四个参数是 从中获取通知的对象

    */

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];

}

/*

 是一个通知方法

 将数据保存,然后终止或者发送到后台

*/

- (void)applicationWillResignActive:(NSNotification *)notification

{

    NSMutableArray *array = [[NSMutableArray alloc]init];

    [array addObject:self.field1.text];

    [array addObject:self.field2.text];

    [array addObject:self.field3.text];

    [array addObject:self.field4.text];

    

    [array writeToFile:[self dataFilePath] atomically:YES];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值