1.IOS沙盒
英文原名为SandBox,简单来说就是为应用程序创建的一段存储空间(存储位置对应成个人PC应该是类似于硬盘的位置),每个应用程序都有自己的沙盒,不允许访问别的应用程序的沙盒,当然别的应用程序也无法访问本应用程序的沙盒.每个应用程序的沙盒的目录结构是分为Documents,Library,tmp三个目录以及一个.app文件,其中.app文件可运行的应用文件,Documents,苹果建议将程序中创建的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录;Library,存储程序存默认设置或其它状态信息;Library/Caches,存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除;tmp,创建和存放临时文件的地方.
2.代码中如何读取目录
获取沙盒根目录:
NSString *dirHome=NSHomeDirectory();
获取Documents目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
获取Library目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
获取caches目录:
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
获取tmp目录:
NSString *tmpDirectory = NSTemporaryDirectory();
获取应用程序根目录(即前面所说的沙盒中的.app文件)
NSString* appPath = [[NSBundle mainBundle] resourcePath];
3.对plist文件的读写操作
因为目前我所接触到的需要我进行操作的文件,都是xml文件格式的,而plist文件其实就是xml文件格式,所以这里我就粗粗谈下我对plist文件读写操作的理解.
关于读取plist文件,既然要读,那么在代码中读出来之后,必然需要用一个数据类型来存储,如果该plist文件的root层是array,那么该文件读出来之后就用NSArray(NSMutableArray)来存,若root层是Dictionary,那么该文件读出来之后就用NSDictionary(NSMutableDictionary)来存.所以读取plist文件存至NSArray或者NSDictionary就由这两个类的方法来实现了.具体怎么存见如下示例代码:
//假设plist文件是存储在Document目录下,则先获取到Document的路径
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//获取名为"arrayType.plist"的plist文件的路径,该plist文件的root层是array类型
//其实就是把documentsDirectory字符串后面append文件名的字符串组成新的字符串
NSString *arrayPlistPath = [documentsDirectory stringByAppendingPathComponent:@"arrayType.plist"];
//通过NSArray类的init方法来读取出该plist文件存入某个NSArray类的对象
NSArray *arrayResult = [[NSArray alloc]initWithContentsOfFile:arrayPlistPath];
NSLog(@"%@", arrayResult);
//假设plist文件是存储在caches目录下,则先获取到caches的路径
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
//获取名为"dicType.plist"的plist文件的路径,该plist文件的root层是Dic类型
NSString *dicPlistPath = [documentsDirectory stringByAppendingPathComponent:@"dicType.plist"];
//通过NSDictionary类的init方法来读取出该plist文件存入某个NSDictionary类的对象
NSDictionary *dicResult = [[NSDictionary alloc]initWithContentsOfFile:dicPlistPath];
NSLog(@"%@", dicResult);
关于写入plist文件,和读取plist文件类似,必须得有一个数据类型存储待写入的数据,这个数据类型一般也就是NSArray和NSDictionary类,所以相类似的也就是由NSArray或者NSDictionary就由这两个类的方法来实现将这两种类的对象写入到plist文件(当然也可以是txt文件,只是写入之后最终生成的文件均是xml格式的),如果目标文件不存在那么貌似会自动新建该文件,不过如果原先有文件,那么内容将被全部覆盖.示例代码如下:
//假设要写入的plist文件是存储在Document目录下,则先获取到Document的路径
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//假设要写入的plist文件名为"arrayType.plist"
NSString *arrayPlistPath = [documentsDirectory stringByAppendingPathComponent:@"arrayType.plist"];
//通过NSArray类的writeToFile方法来将该NSArray类的对象数据写入文件,假设array是一个包含内容的NSArray对象
[array writeToFile:arrayPlistPath atomically:YES];
//假设要写入的plist文件是存储在caches目录下,则先获取到caches的路径
NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [cacPath objectAtIndex:0];
//假设要写入的plist文件名为"dicType.plist"
NSString *dicPlistPath = [documentsDirectory stringByAppendingPathComponent:@"dicType.plist"];
//通过NSDictionary类的writeToFile方法来将该NSArray类的对象数据写入文件,假设dic是一个包含内容的NSDictionary对象
[dic writeToFile:dicPlistPath atomically:YES];
关于新建文件夹和删除文件,这里就必须引入另外一个类NSFileManager,直接上示例代码,大家看着理解理解其中也包含了以NSFileManager方式创建文件:
-(void)createFolder:(NSString *)targetDir
{
BOOL isDir;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:createDir isDirectory:&isDir];
if ( !(isDir == YES && existed == YES) )
{
[fileManager createDirectoryAtPath:createDir withIntermediateDirectories:YES attributes:nil error:nil];
}
}
-(void)createFile:(NSString *) targetDir withFileName:(NSString*) fileName
{
NSString *filePath = [targetDir stringByAppendingPathComponent:fileName];
//文件创建好之后初始的文件内容,这里假设为"My first file in IOS"
NSString *initString = @"My first file in IOS";
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:filePath
contents:[string dataUsingEncoding:NSUTF8StringEncoding]
attributes:nil];
}
-(void)deleteFile:(NSString*) toDeleteFilePath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager removeItemAtPath:toDeleteFilePath error:nil]) {
NSLog(@"Delete Success!");
}
}
4. 我目前用到一种比较特殊的plist文件读写
这里主要说到的就是用库自带的NSUserDefaults类来进行对plist的文件读写和创建.其实这种方式本质上也是对plist文件的读写操作,只是这个plist文件是系统自己管理的,存在于/Library/Prefereces目录中,然后系统给定了一个类似接口的一样的NSUserDefaults类来对这个plist文件进行读写.具体实例代码如下(代码我自己就不写了,网上有现成的就拿来用了~~感谢原作者):
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self saveNSUserDefaults]; //调用此方法将各种数据存储到NSUserDefautls中,在下面定义
[self readNSUserDefaults]; //调用此方法从NSUserDefautls中读取各种数据,在下面定义
}
- (void)viewDidUnload
{
[txtNSString release];
txtNSString = nil;
[txtNSDate release];
txtNSDate = nil;
[txtNSArray release];
txtNSArray = nil;
[txtNSDictionary release];
txtNSDictionary = nil;
[txtInteger release];
txtInteger = nil;
[txtFloat release];
txtFloat = nil;
[txtDouble release];
txtDouble = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[txtNSString release];
[txtNSDate release];
[txtNSArray release];
[txtNSDictionary release];
[txtInteger release];
[txtFloat release];
[txtDouble release];
[super dealloc];
}
//保存数据到NSUserDefaults
-(void)saveNSUserDefaults
{
NSString *myString = @"enuola";
int myInteger = 100;
float myFloat = 50.0f;
double myDouble = 20.0;
NSDate *myDate = [NSDate date];
NSArray *myArray = [NSArray arrayWithObjects:@"hello", @"world", nil];
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"enuo", @"20", nil] forKeys:[NSArray arrayWithObjects:@"name", @"age", nil]];
//将上述数据全部存储到NSUserDefaults中
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//存储时,除NSNumber类型使用对应的类型意外,其他的都是使用setObject:forKey:
[userDefaults setInteger:myInteger forKey:@"myInteger"];
[userDefaults setFloat:myFloat forKey:@"myFloat"];
[userDefaults setDouble:myDouble forKey:@"myDouble"];
[userDefaults setObject:myString forKey:@"myString"];
[userDefaults setObject:myDate forKey:@"myDate"];
[userDefaults setObject:myArray forKey:@"myArray"];
[userDefaults setObject:myDictionary forKey:@"myDictionary"];
//这里建议同步存储到磁盘中,但是不是必须的
[userDefaults synchronize];
}
//从NSUserDefaults中读取数据
-(void)readNSUserDefaults
{
NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
//读取数据到各个label中
//读取整型int类型的数据
NSInteger myInteger = [userDefaultes integerForKey:@"myInteger"];
txtInteger.text = [NSString stringWithFormat:@"%d",myInteger];
//读取浮点型float类型的数据
float myFloat = [userDefaultes floatForKey:@"myFloat"];
txtFloat.text = [NSString stringWithFormat:@"%f",myFloat];
//读取double类型的数据
double myDouble = [userDefaultes doubleForKey:@"myDouble"];
txtDouble.text = [NSString stringWithFormat:@"%f",myDouble];
//读取NSString类型的数据
NSString *myString = [userDefaultes stringForKey:@"myString"];
txtNSString.text = myString;
//读取NSDate日期类型的数据
NSDate *myDate = [userDefaultes valueForKey:@"myDate"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
txtNSDate.text = [NSString stringWithFormat:@"%@",[df stringFromDate:myDate]];
//读取数组NSArray类型的数据
NSArray *myArray = [userDefaultes arrayForKey:@"myArray"];
NSString *myArrayString = [[NSString alloc] init];
for(NSString *str in myArray)
{
NSLog(@"str= %@",str);
myArrayString = [NSString stringWithFormat:@"%@ %@", myArrayString, str];
[myArrayString stringByAppendingString:str];
// [myArrayString stringByAppendingFormat:@"%@",str];
NSLog(@"myArrayString=%@",myArrayString);
}
txtNSArray.text = myArrayString;
//读取字典类型NSDictionary类型的数据
NSDictionary *myDictionary = [userDefaultes dictionaryForKey:@"myDictionary"];
NSString *myDicString = [NSString stringWithFormat:@"name:%@, age:%d",[myDictionary valueForKey:@"name"], [[myDictionary valueForKey:@"age"] integerValue]];
txtNSDictionary.text = myDicString;
}
@end