IOS编程中,有沙盒,在沙盒中有4个储存你数据的地方
1. Doucments文件夹
2.Library文件夹
3.tmp文件夹
4boundle包
下面是具体的例子(有具体注释):
#import "MainViewController.h"
#import "Book.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.
//找沙盒的Documents
NSArray *pathArray =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@",pathArray);
/*
//找沙盒的Library
NSArray *pathArray1 =NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSLog(@"%@",pathArray1);
//找沙盒的tep
NSString * tmp = NSTemporaryDirectory();
NSLog(@"%@",tmp);
//获取bundle包内文件的路径
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"png"];
//系统会把这张照片自动缓存到内存里面,并且释放不掉
// [UIImage imageNamed:@"123"];
//一样可以缓存,会释放掉
[UIImage imageWithContentsOfFile:bundlePath];
NSLog(@"%@",bundlePath);
*/
//写入文件
//字符串写入
[self stromhWrite];
//数组写入
[self arrayWrite];
//字典写入
[self dicWrite];
//读取文件
[self readString];
//data写入
[self writeData];
//读取data
[self readData];
//归档 或叫 序列化
[self writeBookMode];
//解档 或叫 反序列化
[self readBookMode];
}
#pragma mark -
#pragma mark 写入文件
#pragma mark 用字符串写入
- (void)stromhWrite
{
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [docPath lastObject];
path = [path stringByAppendingString:@"/string.txt"];
NSString *str = @"阿黄\n大笨蛋";
NSError *error = nil;
BOOL judge = [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (judge) {
NSLog(@"存储成功");
} else {
NSLog(@"error == %@",error);
}
}
#pragma mark -
#pragma mark 用数组写入
- (void)arrayWrite
{
NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [arrPath lastObject];
path = [path stringByAppendingString:@"/arr"];
NSArray *arr = [NSArray arrayWithObjects:@"oo",@"11",@"22",@"33", nil];
BOOL judge = [arr writeToFile:path atomically:YES];
NSError *error = nil;
if (judge) {
NSLog(@"存储成功");
} else {
NSLog(@"error == %@",error);
}
}
#pragma mark -
#pragma mark 用字典写入
- (void)dicWrite
{
NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [arrPath lastObject];
path = [path stringByAppendingString:@"/dic"];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"aaaaa",@"账号" , @"bbbbb",@"密码", nil];
BOOL judge = [dic writeToFile:path atomically:YES];
NSError *error = nil;
if (judge) {
NSLog(@"存储成功");
} else {
NSLog(@"error == %@",error);
}
}
#pragma mark -
#pragma mark 读取字符串文件
- (void)readString
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingString:@"/string.txt"];
NSError *error = nil;
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",str);
if (str) {
NSLog(@"存储成功");
} else {
NSLog(@"error == %@",error);
}
}
#pragma mark -
#pragma mark 读取数组文件
- (void)readArray
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUTF8StringEncoding, YES) lastObject];
path = [path stringByAppendingString:@"/arr"];
NSArray *arr = [NSArray arrayWithContentsOfFile:path];
NSLog(@"%@",arr);
}
#pragma mark -
#pragma mark 读取字典文件
- (void)readDic
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUTF8StringEncoding, YES) lastObject];
path = [path stringByAppendingString:@"/dic"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"%@",dic);
}
#pragma mark -
#pragma mark 写入data
- (void)writeData
{
//先把字符串转换成data,然后再存进去
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingString:@"/data"];
NSString *str = @"hi,阿黄";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[data writeToFile:path atomically:YES];
}
#pragma mark -
#pragma mark 读取data
- (void)readData
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingString:@"/data"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
#pragma mark -
#pragma mark 归档与解档 或叫 序列化和反序列化
#pragma mark 归档
- (void)writeBookMode
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingString:@"/book"];
Book *book = [Book bookWithName:@"晓峰的一生" time:@"1937.3.8"];
//对单独对象的归档
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:book];
[data writeToFile:path atomically:YES];
// NSArray * arr = [NSArray arrayWithContentsOfFile:book];
// [arr writeToFile:path atomically:YES];
}
#pragma mark 解档
- (void)readBookMode
{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingString:@"/book"];
NSData *data = [NSData dataWithContentsOfFile:path];
Book *book = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"%@ , %@" , book.name , book.time);
}
- (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
#import
@interface Book : NSObject
@property (nonatomic , retain) NSString *name;
@property (nonatomic , retain) NSString *time;
- (instancetype)initWithName:(NSString *)name time:(NSString *)time ;
+ (Book *)bookWithName:(NSString *)name time:(NSString *)time;
@end
#import "Book.h"
@implementation Book
- (void)dealloc
{
[_time release];
_time = nil;
[_name release];
_name = nil;
[super dealloc];
}
- (instancetype)initWithName:(NSString *)name time:(NSString *)time
{
self = [super init];
if (self) {
self.name = name;
self.time = time;
}
return self;
}
+ (Book *)bookWithName:(NSString *)name time:(NSString *)time
{
Book *book = [[[Book alloc] initWithName:name time:time]autorelease];
return book;
}
// 归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.time forKey:@"time"];
}
// 解档
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.time = [aDecoder decodeObjectForKey:@"time"];
}
return self;
}
@end
网友评论