//
// MainViewController.m
// 数据持久化
//
// Created by yangtingting on 14-7-28.
// Copyright (c) 2014年 灭神科技. All rights reserved.
//
#import "MainViewController.h"
#import "Boss.h"
#import "UIImageView+WebCache.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 数据持久化
// 1获取沙盒中文件夹的路径
// 系统的路径函数(支持MACOS和ios开发)
// 参数1: 要获得的文件夾路径
// 参数2: 搜索文件的范围
// 参数3: 选择绝对路径(yes) 还是相对路径(no)
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"搜索文件的路径数组:%@", arr);
// 获取数组中的路径(一般只有一个字符串的对象)
NSString *path = [arr lastObject];
NSLog(@"路径, %@", path);
// 2程序包内部的文件读取(nsbundle)
// 获取bundle对象
NSBundle *bundle = [NSBundle mainBundle];// 单例
// 获取相应文件路径
NSString *docPath = [bundle pathForResource:@"0" ofType:@"jpg"];
NSLog(@"文件路径:%@", docPath);
// 3简单对象写入本地
// 简单对象:系统的字符串(nsstring), 数组, 字典, 数据
// 3.1字符串写入本地
NSString *tempStr= @"国恒伟,是薄凉,送至, 在一起, 求四边形面积";
// 3.2建立一个文件路径
// 在documents文件夾的路径下 进行拼接
NSString *strPath = [path stringByAppendingString:@"/guohengwei.txt"];
// 3.3写入本地的方法
// 参数1:文件的路径
// 参数2:是否对数据读写进行保护
// 参数3:编码方式
// 参数4:错误信息
BOOL b = [tempStr writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"是否成功:%d", b);
// 数组写入本地
NSArray *tempArr = [NSArray arrayWithObjects:@"王鹏贺", @"管萧", @"郭力童", nil];
// 产生路径
NSString *arrPath = [path stringByAppendingPathComponent:@"数组.xml"];
// 写入本地
[tempArr writeToFile:arrPath atomically:YES];
// 字典写入本地
NSDictionary *tempDic = [NSDictionary dictionaryWithObjectsAndKeys:@"管你猜", @"1", @"白庆峰", @"2", nil];
NSString *dicPath = [path stringByAppendingPathComponent:@"字典.plist"];
[tempDic writeToFile:dicPath atomically:YES];
// 从本地获取字典
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
NSLog(@"%@", dic);
/复杂对象写入本地///
// 必须要实现一个协议<NSCoding>
// 魔王类
Boss *boss = [[Boss alloc] init];
boss.name = @"老管";
boss.sex = @"unknown";
// 写入本地
NSString *bossPath = [path stringByAppendingPathComponent:@"boss.aa"];
// 归档类 将一个实现NSCoding协议的对象 写入本地
// 参数1:要写入的对象
// 参数2:写入的路径
BOOL bo = [NSKeyedArchiver archiveRootObject:boss toFile:bossPath];
NSLog(@"%d", bo);
// 反归档类 创建一个对象 利用路径里文件里的数据, 重新创建一个对象
Boss *bossReturn = [NSKeyedUnarchiver unarchiveObjectWithFile:bossPath];
NSLog(@"魔王名号:%@, 魔王性别:%@", bossReturn.name, bossReturn.sex);
// UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
// NSURL *url = [NSURL URLWithString:@"http://image.baidu.com/detail/newindex?col=美女&tag=全部&pn=3&pid=11684594724&aid=&user_id=62593895&setid=-1&sort=0&newsPn=&fr=&from=1"];
// [imageview setImageWithURL:url];
//
// [self.view addSubview:imageview];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
// 引用一个第三方类