文件路径搜索
// 1. 常用的文件夹名称 2.搜索主目录位置 3.是否获取完整的目录
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 获取位置路径
NSString *path = arr[0];
常量
FOUNDATION_EXPORT NSString *NSUserName(void); - 用户名
FOUNDATION_EXPORT NSString *NSFullUserName(void); - 用户全名
注:真机上跟自己的 名称不一样,不知道具体指啥?
FOUNDATION_EXPORT NSString *NSHomeDirectory(void); - 用户主目录,沙盒主目录,使用这个拼接也方便
FOUNDATION_EXPORT NSString *NSTemporaryDirectory(void); - 用户的临时目录
其他沙盒目录需要方法获取。
FOUNDATION_EXPORT NSString * __nullable NSHomeDirectoryForUser(NSString * __nullable userName);
FOUNDATION_EXPORT NSString *NSOpenStepRootDirectory(void);
搜索位置
typedef NS_OPTIONS(NSUInteger, NSSearchPathDomainMask) {
NSUserDomainMask = 1, // 用户目录 - 基本上就用这个。
NSLocalDomainMask = 2, // 本地
NSNetworkDomainMask = 4, // 网络
NSSystemDomainMask = 8, // 系统
NSAllDomainsMask = 0x0ffff // 所有
};
常用文件夹名称
typedef NS_ENUM(NSUInteger, NSSearchPathDirectory) {
- 系统路径下 - NSSystemDomainMask
NSApplicationDirectory = 1, // 应用程序
NSDemoApplicationDirectory, // 应用程序 demo?
NSAdminApplicationDirectory, // 实用工具文件夹
NSLibraryDirectory, // 系统资源库 (Library) - 不是 隐藏的资源库
NSUserDirectory, // 用户 (Users)
NSDeveloperApplicationDirectory, // 已弃用
NSDeveloperDirectory, // 已弃用
- 用户路径 - NSUserDomainMask
NSDocumentationDirectory, // Library 下的(Documentation)模拟器上没有创建
NSDocumentDirectory, // Documents
- 其他 坑爹的
NSCoreServiceDirectory, // location of CoreServices directory (System/Library/CoreServices)
NSAutosavedInformationDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 11, // location of autosaved documents (Documents/Autosaved)
NSDesktopDirectory = 12, // location of user's desktop
NSCachesDirectory = 13, // location of discardable cache files (Library/Caches)
NSApplicationSupportDirectory = 14, // location of application support files (plug-ins, etc) (Library/Application Support)
NSDownloadsDirectory NS_ENUM_AVAILABLE(10_5, 2_0) = 15, // location of the user's "Downloads" directory
NSInputMethodsDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 16, // input methods (Library/Input Methods)
NSMoviesDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 17, // location of user's Movies directory (~/Movies)
NSMusicDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 18, // location of user's Music directory (~/Music)
NSPicturesDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 19, // location of user's Pictures directory (~/Pictures)
NSPrinterDescriptionDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 20, // location of system's PPDs directory (Library/Printers/PPDs)
NSSharedPublicDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 21, // location of user's Public sharing directory (~/Public)
NSPreferencePanesDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 22, // location of the PreferencePanes directory for use with System Preferences (Library/PreferencePanes)
NSApplicationScriptsDirectory NS_ENUM_AVAILABLE(10_8, NA) = 23, // location of the user scripts folder for the calling application (~/Library/Application Scripts/code-signing-id)
NSItemReplacementDirectory NS_ENUM_AVAILABLE(10_6, 4_0) = 99, // For use with NSFileManager's URLForDirectory:inDomain:appropriateForURL:create:error:
NSAllApplicationsDirectory = 100, // all directories where applications can occur
NSAllLibrariesDirectory = 101, // all directories where resources can occur
NSTrashDirectory NS_ENUM_AVAILABLE(10_8, NA) = 102 // location of Trash directory
};
文件类型搜索
// 通过 NSFileManager 的获取文件后续的,筛选工作。
NSArray *fileList = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:homeDirectory error:nil] pathsMatchingExtensions:[NSArray arrayWithObject:@"png"]] ;
其他方法 与 属性
数组 拼接成 路径 - 以及反向
NSString *path = [NSString pathWithComponents:@[@"1",@"2",@"3"]];
NSArray *com = [path pathComponents];
路径末位 操作
@property (readonly, copy) NSString *lastPathComponent;
@property (readonly, copy) NSString *stringByDeletingLastPathComponent;
- (NSString *)stringByAppendingPathComponent:(NSString *)str;
路径后缀(文件类型 .png 等)操作
@property (readonly, copy) NSString *pathExtension;
@property (readonly, copy) NSString *stringByDeletingPathExtension;
- (nullable NSString *)stringByAppendingPathExtension:(NSString *)str;
其他
// 当前string 变成~,OSX上用,app不需要。跟沙箱有关。 @property (readonly, copy) NSString *stringByAbbreviatingWithTildeInPath;
// 格式化 路径??
@property (readonly, copy) NSString *stringByExpandingTildeInPath;
@property (readonly, copy) NSString *stringByStandardizingPath;
@property (readonly, copy) NSString *stringByResolvingSymlinksInPath;
// 拼接 路径
- (NSArray<NSString *> *)stringsByAppendingPaths:(NSArray<NSString *> *)paths;
未知
获取路径下一层 的某些类型的数量,路径,路径数组。(文件夹算1,不管什么类型)
- (NSUInteger)completePathIntoString:(NSString * __nonnull * __nullable)outputName caseSensitive:(BOOL)flag matchesIntoArray:(NSArray<NSString *> * __nonnull * __nullable)outputArray filterTypes:(nullable NSArray<NSString *> *)filterTypes;
NSString *sss;
NSArray *aaa;
NSArray *ttt = @[@"plist",@"text"];
NSUInteger com2 = [path completePathIntoString:&sss caseSensitive:NO matchesIntoArray:&aaa filterTypes:ttt];
@property (readonly) __strong const char *fileSystemRepresentation NS_RETURNS_INNER_POINTER;
- (BOOL)getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max;