IOS数据存储
2015-06-25 20:48
489 查看
// // DataViewController.m // AppUI组件学习 // // Created by 麦子 on 15/6/19. // Copyright (c) 2015年 麦子. All rights reserved. // #import "DataViewController.h" @interface DataViewController (){ UITextView *myText; } @end @implementation DataViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.title = @"数据库学习"; [self createView]; // 查看数据是否存在 NSString *path = NSHomeDirectory(); path = [path stringByAppendingPathComponent:@"login.txt"]; /** 需要注意,中文的时候,需要用 NSUTF16StringEncoding == NSUnicodeStringEncoding 系统生成的时候可能就是 Unicode 创建的。 获取的时候,还是需要这个NSUTF16StringEncoding */ NSError *er = nil; NSString *msg = [NSString stringWithContentsOfFile:path encoding:NSUTF16StringEncoding error:&er]; if (er) { NSLog(@"失败原因:%@",[er description]); } NSLog(@"%@---%@",path,msg); if (msg != nil) { myText.text = msg; }else{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"文件" message:@"登陆信息不存在" delegate:self cancelButtonTitle:@"知道" otherButtonTitles:nil, nil]; [alert show]; } NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; NSString *userName = [def objectForKey:@"userName"]; // [def removeObjectForKey:@"userName"];删除 NSLog(@"---%@",userName); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)createView{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(20, 80, 100, 50); btn.tag = 1; [btn setTitle:@"获取沙河路径" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; UIButton *btnA = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnA.frame = CGRectMake(150, 80, 100, 50); btnA.tag = 2; [btnA setTitle:@"文件保存" forState:UIControlStateNormal]; [btnA addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; myText = [[UITextView alloc] init]; myText.frame = CGRectMake(80, 250, 200, 100); myText.textAlignment = NSTextAlignmentCenter; myText.backgroundColor = [UIColor orangeColor]; myText.textColor = [UIColor redColor]; [self.view addSubview:myText]; [self.view addSubview:btnA]; [self.view addSubview:btn]; } - (void)btnClick:(UIButton *)button{ switch (button.tag) { case 1:{ // 沙盒是一个独立的空间,应用和应用之间的数据不能共享,我们需要保持一些数据的时候,可以在这里创建文件进行记录,保存数据 // 字符串数据保存 NSString *path = NSHomeDirectory(); path = [path stringByAppendingPathComponent:@"login.txt"]; NSString *message = @"我需要保存的数据"; [message writeToFile:path atomically:true]; // documents , library, tmp , 根目录下面的三个目录 NSArray *documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true); NSArray *libraryArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true); // 零时目录,是由系统进行管理的。 NSString *temp = NSTemporaryDirectory(); NSLog(@"%@",temp); break; } case 2:{ // 文件保存,写入数据 // 集合数据保存 NSArray *dataArray = [NSArray arrayWithObjects:@"麦子A",@"麦子B",@"麦子C",@"麦子D",@"麦子E",@"麦子F",@"麦子G", nil]; NSString *path = NSHomeDirectory(); path = [path stringByAppendingPathComponent:@"maizi_today.plist"]; [dataArray writeToFile:path atomically:true]; NSLog(@"%@",path); ; // 字典保存数据 --- 存入的只能是对象 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"小强",@"name",@"男",@"sex",[NSNumber numberWithInt:18],@"age", nil]; NSString *userPath = [NSHomeDirectory() stringByAppendingPathComponent:@"user_message.plist"]; NSLog(@"%@",userPath); [dic writeToFile:userPath atomically:true]; // 自定义类,必须要进行归档,才能存入磁盘。 // NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; [def setValue:@"小名" forKey:@"userName"]; [def synchronize]; break; } default: break; } } @end
相关文章推荐
- IOS 四种保存数据的方式
- sqlite语句
- sqlite3数据存储
- iOS保存数据的4种方式
- ios应用数据存储的常用方式与沙盒
- 沙盒SandBox
- iOS创建、删除文件夹、获取沙盒路径
- 数据存储封装—支持内存和本地缓存
- iOS多线程编程工具
- iOS中的@class
- ios-动画
- 连载六 奖学金文章更新 张文军 北京IOS第38期学员
- 【笔记】iOS开发基础笔记<—>
- iOS 开发学习40 自动布局约束
- IOS block使用中碰到的一个小坑
- iOS开发中学到的技巧
- IOS7 导航栏适配二
- ios 学习之 NSPredicate 模糊、精确、查询
- iOS下FMDB的多线程操作(二)
- iOS下FMDB的多线程操作(二)