技术的乐趣在于分享,欢迎多多交流,多多沟通。
全部博文(877)
分类: iOS平台
2015-08-31 13:19:13
#import "NSString+FilePath.h"
@implementation NSString (FilePath)
//检索指定路径
//第一个参数指定了搜索的路径名称,NSDocumentDirectory表示是在Documents中寻找.NSCacheDirectory的话就是在cache文件中寻找.第二个参数限定了文件的检索范围只在沙箱内部.其意义为用户电脑主目录.也可以修改为网络主机等.最后一个参数决定了是否展开波浪线符号.展开后才是完整路径,这个布尔值一直为YES.
//该方法返回值为一个数组,在iphone中由于只有一个唯一路径(相对OC而言),所以直接取数组第一个元素即可.
+ (NSString *)documentsPath
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
}
//在上一步获得的路径后面加上文件的名字(用字符串的拼接方法).这样使用的返回路径就可以找到指定文件,文件的读取和写入便能够顺利完成了.
+ (NSString *)filePathAtDocumentWithFileName:(NSString *)fileName
{
NSString *filePath = [[self
documentsPath]stringByAppendingPathComponent:fileName];
return filePath;
}
@end
目录类型
NSSearchPathDirectory
enum {
NSApplicationDirectory = 1,//Supported applications (/Applications)
NSDemoApplicationDirectory,//Unsupported applications and demonstration versions
NSDeveloperApplicationDirectory,//Developer applications (/Developer/Applications)
NSAdminApplicationDirectory,//System and network administration applications
NSLibraryDirectory,//Various user-visible documentation, support, and configuration files (/Library)
NSDeveloperDirectory,//Developer resources (/Developer)
NSUserDirectory,//User home directories (/Users)
NSDocumentationDirectory,//
NSDocumentDirectory,//
NSCoreServiceDirectory,//Location of core services (System/Library/CoreServices)
NSAutosavedInformationDirectory = 11,//Location of user’s autosaved documents Library/Autosave Information
NSDesktopDirectory = 12,//
NSCachesDirectory = 13,//Location of discardable cache files (Library/Caches)
NSApplicationSupportDirectory = 14,//Location of application support files (Library/Application Support)
NSDownloadsDirectory = 15,//
NSInputMethodsDirectory = 16,//
NSMoviesDirectory = 17,//
NSMusicDirectory = 18,//
NSPicturesDirectory = 19,//
NSPrinterDescriptionDirectory = 20,//
NSSharedPublicDirectory = 21,//
NSPreferencePanesDirectory = 22,//
NSItemReplacementDirectory = 99,//
NSAllApplicationsDirectory = 100,//
NSAllLibrariesDirectory = 101//
};
typedef NSUInteger NSSearchPathDirectory;
enum {
NSUserDomainMask = 1,//用户主目录中
NSLocalDomainMask = 2,//当前机器中
NSNetworkDomainMask = 4,//网络中可见的主机
NSSystemDomainMask = 8,//系统目录,不可修改(/System)
NSAllDomainsMask = 0x0ffff,//全部
};
typedef NSUInteger NSSearchPathDomainMask;
expandTilde = YES
展开成完整路径