本地化 存储 字符串 数组 字典 复杂类对象
2014-07-31 14:20
246 查看
//
// 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
// 引用一个第三方类
// 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
// 引用一个第三方类
相关文章推荐
- 字符串,数组和字典的简单本地存储!以及复杂类型对象的存储
- .net Mvc Controller 接收 Json/post方式 数组 字典 类型 复杂对象
- JSON:JSON对象和JSON数组混排的复杂字符串
- 二十六、数组、指针和字符串:数组的存储与初始化、对象数组、数组作为函数参数
- iOS开发:沙盒机制以及利用沙盒存储字符串、数组、字典等数据
- iOS对象(字典或数组)转化为JSon字符串
- iOS开发:沙盒机制以及利用沙盒存储字符串、数组、字典等数据
- C++编程入门系列之二十六(数组、指针和字符串:数组的存储与初始化、对象数组、数组作为函数参数)
- 关于本地缓存localstorage与sessionStorage 数组 (array)字符串(string) 对象(object)的存储技巧和注意事项
- iOS对象(字典或数组)转化为JSon字符串
- iOS开发:沙盒机制以及利用沙盒存储字符串、数组、字典等数据
- iOS开发:沙盒机制以及利用沙盒存储字符串、数组、字典等数据
- 关于本地缓存localstorage与sessionStorage 数组 (array)字符串(string) 对象(object)的存储技巧和注意事项
- sessionStorage存储数据(对象、数组、字符串)
- string对象代替char数组实现字符串的组合存储并显示
- post发送JSON数据(字符串、数组、字典、自定义对象)给服务器
- ios 字符串,字典,数组存储到本地文件
- memcache存储 字符串、数组、及其对象
- .net Mvc Controller 接收 Json/post方式 数组 字典 类型 复杂对象
- \t\t.net Mvc Controller 接收 Json/post方式 数组 字典 类型 复杂对象