沙盒

沙盒

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

// 数据持久化

// 一些不可再生的数据可以进行数据持久化,说白了就是放进APP的文件中,而可再生的数据,是放在内存中的;可以被适当

// 沙盒(SandBox),他的本质是一个文件夹,叫沙盒的原因是因为它符合一种安全机制


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
   
    // 沙盒路径 : 每次安装应用程序,都会产生一个沙盒,沙盒的路径非常深,并且经过了随机加密
   
    // 沙盒的使用,导致APP都是自身管理自身,在没有权限或者许可的情况下,两个不同的APP不可以互相访问
   
    // 1.
//    NSString *userName = NSUserName();
//    NSString *sandBoxPath = NSHomeDirectoryForUser(userName);
//    
//    NSLog(@"%@",sandBoxPath);
   
    // 2.
    NSString *sandBoxPath = NSHomeDirectory();
    NSLog(@"%@",sandBoxPath);
    // 沙盒文件
 /*
    // 沙盒中有三个文件夹
       // 1. Document    存储用户数据,需要备份的信息 (一般是那些被数据持久化的数据)
       // 会被iTunes同步
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"%@",documentPath);
   
        // 2. Library/Caches 存储缓存文件 (一般是一些图片和视频的缓存)
        // 不会被iTunes不会被同步
    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"%@",cachesPath);
   
        // 3. Library/Preferences 存储应用程序的偏好设置文件
        // 会被iTunes同步
    NSString *preferencesPath = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, NO)[0];
       // 如果把参数改成NO 沙盒路径前面会省略用波浪线代替
    NSLog(@"%@",preferencesPath);
   
       // 4. tmp 临时文件夹,放临时文件,退出APP的时候,里面文件被清除
       // 不会被iTunes同步
    NSString *tempPath = NSTemporaryDirectory();
    NSLog(@"%@",tempPath);
   
        // 5. iOS8之前,沙盒还有一个.app的文件夹
    NSString *appPath = [[NSBundle mainBundle] bundlePath];
    NSLog(@"%@",appPath);
   
  */

简单对象写入文件

    /*
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
   
    NSString *filePath  = [NSString stringWithFormat:@"%@/张三的战书.txt",documentPath];
    NSString *loveContent = @"iLoveU";
    // 将内容写入文件
    [loveContent writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@",filePath);
    NSString *filePath2 =  [documentPath stringByAppendingString:@"/myVideo.avi"];
    NSString *aviContent = @"爱情动作片";
    [aviContent writeToFile:filePath2 atomically:YES encoding:NSUTF8StringEncoding error:nil];
   NSLog(@"%@",filePath2);

    // 新建一个文件
   
    // 写入一个简单对象 NSArray NSDictionAry
    NSArray *arr = [NSArray arrayWithObjects:@"da",@"fag",@"afgthfd", nil];
    [arr writeToFile:filePath atomically:YES];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"a",@"2",@"b",@"3",@"c", nil];
    [dict writeToFile:filePath atomically:YES];
   
    NSLog(@"%@",filePath);
   
    // 读取文件中的内容
    */
   

NSFileManager 文件管理者

 //1. 新建文件夹 2. 可以创建 移动 复制 删除文件   3.判断文件是否存在

 NSFileManager *manager = [NSFileManager defaultManager];

 // 1. 新建文件夹
 [manager createDirectoryAtPath:[sandBoxPath stringByAppendingString:@"/我的"]withIntermediateDirectories:YES attributes:nil error:nil];
 NSLog(@"%@",sandBoxPath);

 // 删除
// [manager removeItemAtPath:[sandBoxPath stringByAppendingString:@"/我的"] error:nil];

 // 移动
//  [manager moveItemAtPath: toPath: error:;]

 // 复制
//   [manager copyItemAtPath: toPath: error:];

 // 判断是否存在
//    - (BOOL)fileExistsAtPath:(NSString *)path;
//    - (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory;
//    - (BOOL)isReadableFileAtPath:(NSString *)path;
//    - (BOOL)isWritableFileAtPath:(NSString *)path;
//    - (BOOL)isExecutableFileAtPath:(NSString *)path;
//    - (BOOL)isDeletableFileAtPath:(NSString *)path;

}

复杂对象写入沙盒

#import <Foundation/Foundation.h>

// 1. 遵循协议
@interface Person : NSObject<NSCoding>

@property (nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *gender;


@end


#import "Person.h"

@implementation Person

// 实现NSCoding的编码和反编码方法

// 编码方式
- (void)encodeWithCoder:(NSCoder *)aCoder{
    // 把model类里面的属性进行编码
    [aCoder encodeObject:self.name  forKey:@"p_name"];
    [aCoder encodeObject:self.gender forKey:@"p_gender"];
}

// 反编码方法
- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        // 反编码的时候的时候要找返回值接受
   self.name = [aDecoder decodeObjectForKey:@"p_name"];
    self.gender =  [aDecoder decodeObjectForKey:@"p_gender"];
    };
    return self;
}

@end



#import "RootViewController.h"
#import "Person.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
// document路径
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,  YES)[0];
    NSString *filePath = [documentPath stringByAppendingString:@"/小明.txt"];
//    NSString *tmpStr = @"我爱台妹";
//    
//    [tmpStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//    
    Person *p1 = [[Person alloc]init];
   
    // 由于p1是复杂对象,没有办法直接写入文件
   
    // 简单对象可以直接写入对象的原因是因为NSString,NSArray,NSdictionary,NSNumber  这些对象他们遵循了NSCoding 协议
   
    p1.name  = @"张三";
    p1.gender = @"男";
   
    NSMutableData *data1 = [NSMutableData data];
   
    // 归档工具
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data1];
   
    [archiver encodeObject:p1 forKey:@"person"];
    [archiver finishEncoding];
   
    [data1 writeToFile:filePath atomically:YES];
    NSLog(@"%@",filePath);
   
    // 读取 (反归档)
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data1];
    Person *p2 =  [unarchiver decodeObjectForKey:@"person"];
    NSLog(@"name:%@ gender:%@",p2.name,p2.gender);
    [unarchiver finishDecoding];
 
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容