您的位置:首页 > 产品设计 > UI/UE

iOS中的UI数据持久化,沙盒

2015-12-07 20:19 393 查看
//

// RootViewController.m

// UI18_01数据持久化

//

// Created by Rickie_Lambert on 15/12/7.

// Copyright (c) 2015年 Rickie. All rights reserved.

//

#import "RootViewController.h"

@interface
RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view.



self.view.backgroundColor = [UIColor
whiteColor];



//iOS沙盒机制:每个应用程序都有一个沙盒,而且只允许当前应用程序进行访问。

//沙盒的本质:是一个文件夹。
沙盒:sandbox

/*

Discussion

In iOS, the home directory is the application’s sandbox directory. In OS X, it is the application’s sandbox directory or the current user’s home directory (if the application is not in a sandbox).

For more information on file-system utilities, see Low-Level File Management Programming Topics.

讨论

在iOS中,主目录是应用程序沙盒目录。在操作系统中,它是应用程序的“目录”或当前用户的主目录(如果应用程序不在一个)。

有关文件系统实用程序的更多信息,请参见低级别文件管理程序设计主题。

*/

NSString *path = NSHomeDirectory();

NSLog(@"��NSHomeDirectory:%@", path);



//沙河内的文件结构:

//1、Documents:主要用来存储用户数据,或者需要同步上传iTunes或者iCloud的数据。

//2、Library:

//Caches:用来存储缓存文件

//Preferences:存储偏好设置文件

//3、tmp:主要用来存储临时文件
特点:系统会不定期清理该文件夹



#pragma mark 第一步、

//找到当前沙盒内某个文件夹

/**

* 参数说明

*

* @param NSDocumentDirectory 1、指定的文件夹

* @param NSUserDomainMask 2、当前沙盒的主目录

* @param YES 3、将当前路径是否以“~”展开,
通常都为YES;该参数的作用是否获得相对路径;相对路径只能在当前文件夹内才能找到某个文件夹或文件

*

*/

NSString *documentPath =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES).lastObject;



NSLog(@"��===文件夹路径:%@",
documentPath);



#pragma mark 第二步、

//创建新文件路径,拼接要写的文件的绝对路径 stringByAppendingString
一层文件

NSString *firstFile = [documentPath
stringByAppendingString:@"/first.txt"];

#pragma mark 第三步、写入沙盒文件

//在文件内写内容

// NSString *string = @"这是写入沙盒内的第一句话";

#pragma mark 第四步、再次在这个文件中写入内容,但不覆盖之前已经写过的内容,就需要用万能的拼接方法(stringWithFormat:)

/**********************
注释 **********************/

NSString *string2 =
@"今天真高兴";

NSString *perString = [NSString
stringWithContentsOfFile:firstFile encoding:NSUTF8StringEncoding
error:nil];

//第一个%@代表:��先前的字符串
第二个%@代表:��后来添加的字符串(即string2)

NSString *lastString = [NSString
stringWithFormat:@"%@,%@===", perString, string2];

/**********************
注释 **********************/



#pragma mark 第五步:

#warning 注意:如果往文件中写入数据,那么数据会将文件内的原数据覆盖掉。

// [string writeToFile:firstFile atomically:YES encoding:NSUTF8StringEncoding error:nil];

//将字符串对象写入沙盒文件

#pragma mark atomically 原子性
限制多线程下访问文件的安全性

//这里前面的BOOL result
可以不要

BOOL result = [lastString
writeToFile:firstFile atomically:YES
encoding:NSUTF8StringEncoding
error:nil];





#pragma mark 第六步:

//从沙盒内的文件读取内容

NSString *readString = [NSString
stringWithContentsOfFile:firstFile encoding:NSUTF8StringEncoding
error:nil];

//打印写入firstFile(即这个first.txt文件中的内容)

NSLog(@"����:%@",
readString);





/**********************
往Caches文件夹下写入字符串 **********************/

#warning 注意:文件路径的第二种拼接方法,
直接写文件名即可,不需要“/”, stringByAppendingPathComponent

//第一步:创建文件夹路径cachesPath

NSString *cachesPath =
NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES).lastObject;

//第二步:在这个文件夹路径下,创建一个html文件

NSString *filePath = [cachesPath
stringByAppendingPathComponent:@"second.html"];

//第三步:在这个html文件中写入内容
汉字会显示乱码,英文正常显示

NSString *secondString =
@"周六约吗, you are right";

//第四步:将要写入的内容添加到文件中

[secondString writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];



/**********************
数组的读写 **********************/



//第一步:

NSString *tempPath =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES).lastObject;



//第二步:

NSString *tempFile = [tempPath
stringByAppendingString:@"/array.plist"];



//第三步:

NSArray *array =
@[@"小张",
@"小王",
@"小李",
@"小狗",
@"小明"];



//第四步:

BOOL resultArray = [array
writeToFile:tempFile atomically:YES];



//第五步:

if (resultArray) {

NSArray *arr = [NSArray
arrayWithContentsOfFile:tempFile];

NSLog(@"��数组:%@,这是文件路径
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: