沙盒:
1.documents 数据
2.app app本身
3. tmp 临时文件夹
4.library 资源库
(1)cache 程序专用的支持文件
(2)preferences 引用程序偏好设置
工程路径: 工程路径指的是开发人员在自己的电脑上创建的文件夹的路径
沙盒路径: 沙盒路径指的是本地的sandbox文件的路径 系统单例:
//当前应用程序对象(可以用来专递AppDelegate中声明的一些值)
//系统引用 UIApplication * app=[UIApplication sharedApplication];
//系统引用代理 AppDelegate * appDelegate=[app delegate];
//应用程序窗口 UIWindow * w=appDelegate.window;
//用户偏好设置 NSUserDefaults * user=[NSUserDefaults standardUserDefaults];
//主工程束 NSBundle * mainB=[NSBundle mainBundle];
//文件管理者 NSFileManager * fileManager1=[NSFileManager defaultManager]; //主屏幕 UIScreen * screen=[UIScreen mainScreen];
沙盒路径操作 //===========================沙盒路径操作============================= //用户根目录 NSString * homePath=NSHomeDirectory(); NSLog(@"homePath=%@",homePath);
//documents目录(在当前用户权限域内搜索,ios数组中只有一个成员,mac下可能会有多个)
NSString * documentsPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSLog(@"documentsPath=%@",documentsPath);
//library目录
NSString * libraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSLog(@"library=%@",libraryPath);
//cache目录
NSString * cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]; NSLog(@"cachePath=%@",cachePath);
//tmp目录
NSString * tmpPath=NSTemporaryDirectory();
NSLog(@"tmpPath=%@",tmpPath);
//app目录
NSString * studentPath=[[NSBundle mainBundle]pathForResource:@"studnet" ofType:@"xml"];
NSLog(@"studentPath=%@",studentPath);
NSString * appPath=[[NSBundle mainBundle]resourcePath];
NSLog(@"appPath=%@",appPath);
NSLog(@"%@",[NSBundle allBundles]);
NSLog(@"%@",[NSBundle allFrameworks]);
NSLog(@"%@",[[NSBundle mainBundle] executablePath]);
NSLog(@"%@",[[NSBundle mainBundle] bundlePath]);
//preferences 用户偏好设置
NSUserDefaults * userDefault=[NSUserDefaults standardUserDefaults];
[userDefault setBool:YES forKey:@"背景灯"];
[userDefault setInteger:23 forKey:@"20"];
[userDefault synchronize];//同步数据 文件读写操作:
//******************字符串读写****************************
NSString * str=@"こんにちは、王冬云だった";
//创建文件路径
NSString * filePath_str=[documentsPath stringByAppendingPathComponent:@"str.txt"];
//写入文件(如果文件不存在,先创建对应的文件,然后写入)
NSError * error=nil;
[str writeToFile:filePath_str atomically:YES encoding:NSJapaneseEUCStringEncoding error:&error];
if (error) { NSLog(@"写入文件失败!"); }
//从文件中读 //按指定编码方式读 NSString * str2=[[NSString alloc]initWithContentsOfFile:filePath_str encoding:NSJapaneseEUCStringEncoding error:nil]; NSLog(@"str2=%@",str2);
//读文件,同时返回它的编码方式
NSStringEncoding encode;
NSString * str3=[[NSString alloc]initWithContentsOfFile:filePath_str usedEncoding:&encode error:nil];
NSLog(@"encode=%u",encode);
NSLog(@"str3=%@",str3);
//************************数组的读写************************
NSArray * array=@[@"iphone",@"android",@"symbain"];
NSString * arrayFilePath=[documentsPath stringByAppendingFormat:@"/array.txt"];
//写入文件 [array writeToFile:arrayFilePath atomically:YES];
//读取文件
NSArray * array2=[[NSArray alloc]initWithContentsOfFile:arrayFilePath];
NSLog(@"array2=%@",array2);
//************************字典的读写***********************
NSDictionary * dict1=@{@"name": @"陈芬",@"age":@"18",@"gender":@"女"};
NSString * dicFilePath=[documentsPath stringByAppendingPathComponent:@"dict.txt"];
//写入文件
[dict1 writeToFile:dicFilePath atomically:YES];
//从文件中读
NSDictionary * dict2=[[NSDictionary alloc]initWithContentsOfFile:dicFilePath];
NSLog(@"dict2=%@",dict2);
//********************NSData的读写************************
//UIImage的读方法
NSString * imageFilePath=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"png"];
UIImage * image1=[UIImage imageWithContentsOfFile:imageFilePath];
//NSData从文件中读
NSData * imageData1=[NSData dataWithContentsOfFile:imageFilePath];
NSString * document_image_path=[documentsPath stringByAppendingPathComponent:@"xcode.png"];
//NSData数据写入文件
[imageData1 writeToFile:document_image_path atomically:YES];
//PS:NSData可以通过文件读把任何类型转化成NSData类型,也可以写入文件
//******************UIImage和NSData之间的相互转化************
//把UIImage类型转化成NSData类型
NSData * imageData2=UIImageJPEGRepresentation(image1, 1);
NSData * imageData3=UIImagePNGRepresentation(image1);
//把一个图片的二进制NSData转化成一个UIImage类型
UIImage * image=[UIImage imageWithData:imageData2];
//********************字符串和NSData的之间的相互转化**************
NSString * name=@"刘璇";
//字符串转化成NSData
NSData * data=[name dataUsingEncoding:NSUTF8StringEncoding];
//NSData转化成字符串
NSString * name2=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"name2=%@",name2);
NSString * name3=[NSData dataWithBytes:[data bytes] length:10];
NSLog(@"name3=%@",name3);