IOS數(shù)據(jù)儲(chǔ)存

IOS編程中,有沙盒,在沙盒中有4個(gè)儲(chǔ)存你數(shù)據(jù)的地方

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包內(nèi)文件的路徑

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"png"];

//系統(tǒng)會(huì)把這張照片自動(dòng)緩存到內(nèi)存里面,并且釋放不掉

//? ? [UIImage imageNamed:@"123"];

//一樣可以緩存,會(huì)釋放掉

[UIImage imageWithContentsOfFile:bundlePath];

NSLog(@"%@",bundlePath);

*/

//寫入文件

//字符串寫入

[self stromhWrite];

//數(shù)組寫入

[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(@"存儲(chǔ)成功");

} else {

NSLog(@"error == %@",error);

}

}

#pragma mark -

#pragma mark 用數(shù)組寫入

- (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(@"存儲(chǔ)成功");

} 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",@"賬號(hào)" , @"bbbbb",@"密碼", nil];

BOOL judge = [dic writeToFile:path atomically:YES];

NSError *error = nil;

if (judge) {

NSLog(@"存儲(chǔ)成功");

} 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(@"存儲(chǔ)成功");

} else {

NSLog(@"error == %@",error);

}

}

#pragma mark -

#pragma mark 讀取數(shù)組文件

- (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

{

//先把字符串轉(zhuǎn)換成data,然后再存進(jìn)去

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"];

//對(duì)單獨(dú)對(duì)象的歸檔

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容