1.获取沙盒路径
调用方式 [self getHomePath];
-(NSString *)getHomePath//自定义函数名,返回字符串
{
NSString *homePath =NSHomeDirectory();//系统函数
NSLog(@"homePath = %@",homePath);
return homePath;
}
2.获取documents路径
//获取documents路径
-(NSString *)getDocumentPath
{
//第一个参数指定了搜索的路径名称
//第二个参数限定了在沙盒内部
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [docPaths lastObject];
NSLog(@"documentsPath = %@",documentsPath);
return documentsPath;
}
3.获取library路径
//获取Library路径
-(NSString *)getLibraryPath
{
//第一个参数指定了搜索的路径名称
//第二个参数限定了在沙盒内部
NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryPath = [libPaths lastObject];
NSLog(@"libraryPath = %@",libraryPath);
return libraryPath;
}
3.获取Tmp路径
//获取tmp文件夹路径
-(NSString *)getTmpPath
{
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"tmpPath = %@",tmpPath);
return tmpPath;
}
4.获取路径的各个组成部分
//获取路径的各个组成部分
-(void)parsePath
{
NSString *path = @"/Users/zero.jin/Library/";//待分割目标路径
NSArray *pathArray = [path pathComponents];
NSLog(@"pathComponents = %@",pathArray);
//提取路径的最后一个组成成分即文件名
NSString *name = [path lastPathComponent];
//提取文件的路径
NSString *filePath = [path stringByDeletingLastPathComponent];
//追加文件名
NSString *addStr = [path stringByAppendingPathComponent:@"haha.txt"]
}
5.各种数据之间的转换
//数据之间的转换,NSData表示二进制数据,示例在于形,不纠结于具体的参数
-(void)dataChange:(NSData *)data
{
//NSData -> NSString
NSString *aString = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)];
//NSString ->NSData
NSData *aData = [aString dataUsingEncoding:NSUTF8StringEncoding];
//NSData ->UIImage
UIImage *image = [UIImage imageWithData:aData];
//UIImage ->NSData
NSData *imageData = UIImagePNGRepresentation(image);
}
6.创建文件夹
//创建文件夹
-(void)createFolder
{
NSString *docPath = [self getDocumentPath];
NSString *testPath = [docPath stringByAppendingPathComponent:@"myFolder"];
NSFileManager *manager = [NSFileManager defaultManager];
//withIntermediateDirectories YES代表覆盖原文件,NO表示不覆盖
BOOL ret = [manager createDirectoryAtPath:testPath withIntermediateDirectories:(YES) attributes:nil
error:nil];
}