保存数据

-(NSString *)dataFilePath{
    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory =[paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFilename];
}



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString * filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
        NSArray * array_ = [[NSArray alloc]initWithContentsOfFile:filePath];
        text1.text = [array_ objectAtIndex:0];
        text2.text = [array_ objectAtIndex:1];
        text3.text = [array_ objectAtIndex:2];
        text4.text = [array_ objectAtIndex:3];
    }
    UIApplication * app = [UIApplication sharedApplication];
	
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];// Do any additional setup after loading the view.
}



-(void)applicationWillResignActive:(NSNotification *)notifacation{
    NSMutableArray * array_ = [[NSMutableArray alloc]init];
    [array_ addObject:text1.text];
    [array_ addObject:text2.text];
    [array_ addObject:text3.text];
    [array_ addObject:text4.text];
    [array_ writeToFile:[self dataFilePath] atomically:YES];
}

读取或者新创建的文件在application下得documents里面;


NSFileManager常见方法:

(1)-(NSString *)currentDirectoryPath  获取当前目录

  (2)-(BOOL)changeCurrentDiretoryPath:path 更改当前目录

  (3)-(BOOL)createDirectoryAtPath:path withIntermediate Directories:(BOOL)flag attributes:attr   创建一个新目录

  (4)-(BOOL)copyItemAtPath:from toPath: to error:err  复制目录结构(to不能已经存在)

  (5)-(NSArray *)contentsOfDIrectoryAtPath:path error:err 列出目录内容


 常用查看ios目录

(1)Documents (NSDocumentDirectory) 用于写入应用相关数据文件

(2)Library/Cashes(NSCachesDirectory) 写入应用支持文件的目录

(3)tmp(use NSTemporaryDirecory())) 临时文件




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值