------------------------------ViewController.m------------------------------
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark --- 獲取沙盒文件夾的路徑
//NSDocumentDirectory 表示要獲取Document文件夾的地址
//NSUserDomainMask 表示用戶的主目錄
//第三個(gè)參數(shù)表示展開"~"的地址,設(shè)置為YES 為完整的路徑
//NSSearchPathForDirectoriesInDomains獲取的是一個(gè)數(shù)組,數(shù)組只有一個(gè)元素,所以可以直接獲取objectAtIndex:0
// 第一種獲取文件夾地址的方式,這種方法適用于沙盒里documents和library文件夾,其他文件夾不可以
//NSString *documentPathStr = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//NSLog(@"%@",documentPathStr);
//第二種方式獲取documents文件夾的路徑,這種方法也適用于沙盒里的其他文件(library,tmp,caches,preferences等),對路徑來說,字母大小寫沒區(qū)別
//第一步:獲取沙盒主路徑的地址
//NSString *homePathStr = NSHomeDirectory();
//NSLog(@"homePath ==== %@",homePathStr);
//第二步:在沙盒主路徑后拼接Documents,拼接出來documents文件夾的路徑
//NSString *documentPathStr = [homePathStr stringByAppendingPathComponent:@"library/preferences"];
//NSLog(@"documentPathStr ==== %@",documentPathStr);
//獲取tmp文件夾的路徑,還可以用上面第二種方法來獲取tmp文件夾路徑:先獲取沙盒路徑,再拼接出來tmp文件夾的路徑
//NSString *tmpPathStr = NSTemporaryDirectory();
//NSLog(@"tmpPathStr ==== %@",tmpPathStr);
#pragma mark --- 字符串的存儲(chǔ)
//1:我要知道存到哪里,所以需要一個(gè)文件夾的路徑
NSString *documentPathStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//2:我要知道要存什么,所以創(chuàng)建一條數(shù)據(jù)
NSString *str = @"hello world";
//3:我要知道我存的東西到底放在哪里,所以創(chuàng)建了一個(gè)路徑,路徑的終點(diǎn)就是存數(shù)據(jù)的文件
NSString *strPath = [documentPathStr stringByAppendingPathComponent:@"str.plist"];
//plist文件不能直接存放字符串
//4:準(zhǔn)備工作做好了,開始寫入的操作
[str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSLog(@"document === %@",documentPathStr);
//通過路徑讀取數(shù)據(jù),使用stringWithContentsOfFile方法,在讀取的時(shí)候,第一個(gè)參數(shù)表示讀取文件的路徑,第二個(gè)參數(shù)表示編碼格式,第三個(gè)表示錯(cuò)誤信息
NSString *newStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"newStr ==== %@",newStr);
#pragma mark --- 數(shù)組的存儲(chǔ)
//2:準(zhǔn)備存儲(chǔ)的數(shù)據(jù)
NSArray *array = @[@"1",@"2",@"3"];
//3:存在哪里
NSString *arrayPath = [documentPathStr stringByAppendingPathComponent:@"array.plist"];
//4:寫入數(shù)據(jù)
[array writeToFile:arrayPath atomically:YES];
//通過路徑讀取數(shù)組,使用arrayWithContenesOfFile
NSArray *newArray = [NSArray arrayWithContentsOfFile:arrayPath];
NSLog(@"newArray ==== %@",newArray);
#pragma mark- 存儲(chǔ)字典
//準(zhǔn)備存儲(chǔ)的數(shù)據(jù)
NSDictionary *dic = @{@"a":@"1",@"b":@"2"};
//存在哪里
NSString *dicPath = [documentPathStr stringByAppendingPathComponent:@"dic.plist"];
//寫入數(shù)據(jù)
[dic writeToFile:dicPath atomically:YES];
//通過路徑讀取數(shù)組,使用dictionaryWithContentsOfFile
NSDictionary *newDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
NSLog(@"newDic ==== %@",newDic);
#pragma mark- NSData存儲(chǔ)
//根據(jù)imageNamed獲取圖片時(shí)會(huì)在緩存里面存一份,下次再獲取同名圖片的話直接從緩存里取.優(yōu)點(diǎn):快,只有第一次的時(shí)候稍慢,但是之后再去獲取的話會(huì)很快.缺點(diǎn):會(huì)浪費(fèi)內(nèi)存,如果只用一次的話這塊內(nèi)存就浪費(fèi)掉了.
//UIImage *image = [UIImage imageNamed:(nonnull NSString *)];
//根據(jù)ContentsOfFile獲取圖片時(shí)每一次都會(huì)根據(jù)路徑去取圖片,不會(huì)占用內(nèi)存.如果這個(gè)圖片只使用一次的話,推薦使用ContentsOfFile
//UIImage *image = [[UIImage alloc] initWithContentsOfFile:(nonnull NSString *)];
//123.png
//123@2X.png
//123@3X.png
UIImage *image = [UIImage imageNamed:@"123"];
//UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//[self.view addSubview:imageView];
//將UIImage類型對象轉(zhuǎn)化成NSData類型的
//第一個(gè)參數(shù):轉(zhuǎn)哪個(gè)UIImage類型的對象
//第二個(gè)參數(shù):壓縮系數(shù),越小壓縮的越厲害
NSData *data = UIImageJPEGRepresentation(image, 1);
//將data存入到本地
NSString *dataPath = [documentPathStr stringByAppendingPathComponent:@"123.png"];
[data writeToFile:dataPath atomically:YES];
//讀取本地的data
NSData *newData = [NSData dataWithContentsOfFile:dataPath];
//將讀取出來的data放在imageView上顯示
UIImage *newImage = [[UIImage alloc] initWithData:newData];
UIImageView *imageView = [[UIImageView alloc] initWithImage:newImage];
[self.view addSubview:imageView];
NSString *newPath = [documentPathStr stringByAppendingPathComponent:@"/new/newStr.txt"];
//這里"/new/newStr.txt"只是文件路徑,如果documents文件夾下沒有"new"這個(gè)文件夾的話,那么運(yùn)行程序以后這里創(chuàng)建路徑就會(huì)失敗,然后后面數(shù)據(jù)也存不到本地.這句程序是不會(huì)創(chuàng)建出一個(gè)名稱為"new"的文件夾的.如果沒有"new"這個(gè)文件夾,可以在documents文件夾下手動(dòng)新建一個(gè)名稱為"new"的文件夾,這樣程序就可以正常運(yùn)行了.
NSLog(@"newPath ==== %@", newPath);
NSString *newStr1 = @"newStr1";
NSError *error = nil;
[newStr1 writeToFile:newPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"error = %@",error);
NSString *newStr2 = [NSString stringWithContentsOfFile:newPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"newStr2 ==== %@",newStr2);
#pragma mark- 復(fù)雜對象存儲(chǔ)
//如果一個(gè)對象想直接寫入本地,那么這個(gè)對象需要遵守NSCoding協(xié)議
Person *person = [[Person alloc] init];
person.name = @"張三";
person.gender = @"男";
person.age = 38;
//將復(fù)雜對象歸檔之后存入本地
//第一步:創(chuàng)建一個(gè)NSMutableData,歸檔后的數(shù)據(jù)放到這里面
NSMutableData *data1 = [NSMutableData data];
//第二步:創(chuàng)建一個(gè)歸檔工具
NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data1];
//第三步:使用歸檔工具 對需要?dú)w檔的對象進(jìn)行歸檔
[keyedArchiver encodeObject:person forKey:@"person"];
//第四步:結(jié)束歸檔
[keyedArchiver finishEncoding];
NSLog(@"data1 ==== %@",data1);
//創(chuàng)建存放數(shù)據(jù)的路徑
NSString *mutableDataPath = [documentPathStr stringByAppendingPathComponent:@"person.plist"];
//將data1寫入路徑里的文件中,這里data1的內(nèi)容就是對象歸檔后得到的數(shù)據(jù)
[data1 writeToFile:mutableDataPath atomically:YES];
//解檔并使用
//第一步:從本地獲取到data1
NSData *newData1 = [NSData dataWithContentsOfFile:mutableDataPath];
//第二步:創(chuàng)建一個(gè)解檔工具,并且設(shè)置對newData1解檔
NSKeyedUnarchiver *keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:newData1];
//第三步:創(chuàng)建一個(gè)person對象,接收解檔結(jié)果
Person *newPerson = [keyedUnarchiver decodeObjectForKey:@"person"];
//第四步:結(jié)束解檔
[keyedUnarchiver finishDecoding];
NSLog(@"person.name ==== %@",newPerson.name);
/*
歸解檔是一種編碼方式,不是數(shù)據(jù)本地化的方式
復(fù)雜對象寫入本地實(shí)際上使用的還是writeToFile的簡單寫入本地的方法
直接寫入本地 是整存整取
在一個(gè)路徑下存數(shù)據(jù),最后一次存進(jìn)去的東西會(huì)覆蓋掉之前的
*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
@end
---------------------------------Person.n-----------------------------------
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *gender;
@property (nonatomic, assign) NSUInteger age;
@end
---------------------------------Person.m-----------------------------------
#import "Person.h"
@implementation Person
//歸檔
//在歸檔和解檔的時(shí)候,要把所有的屬性都進(jìn)行歸解檔
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.gender forKey:@"gender"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
//解檔
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init])
{
self.name = [aDecoder decodeObjectForKey:@"name"];
self.gender = [aDecoder decodeObjectForKey:@"gender"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
123.png:
