NSPathUtilities

本文深入解析NSString类在处理路径字符串时提供的多种实用方法,包括路径拼接、截取、标准化等,以及如何获取用户和系统目录路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pragma mark - NSString(NSStringPathExtensions)

pathWithComponents:

/**
 *  拼接地址
 *  @param components 传入NSString数组
 *  如传入@[@"www.baidu.com",@"ct",@"108"],输出www.baidu.com/ct/108
 *  @return 拼接后的地址
 */
+ (NSString *)pathWithComponents:(NSArray<NSString *> *)components;

pathComponents

/** 将路径中的首地址和资源路径截取 */
@property (readonly, copy) NSArray<NSString *> *pathComponents;

absolutePath

/** 是否为绝对路径 */
@property (getter=isAbsolutePath, readonly) BOOL absolutePath;

lastPathComponent

/** 最后一个资源路径 */
@property (readonly, copy) NSString *lastPathComponent;

stringByDeletingLastPathComponent

/** 整个路径最后一个字符为'/',则删除掉这个字符 */
@property (readonly, copy) NSString *stringByDeletingLastPathComponent;

stringByAppendingPathComponent:

/** 最尾部拼接一个资源路径 */
- (NSString *)stringByAppendingPathComponent:(NSString *)str;

pathExtension

/** 路径下的文件扩展名 */
@property (readonly, copy) NSString *pathExtension;

stringByDeletingPathExtension

/** 删除路径下的文件扩展名 */
@property (readonly, copy) NSString *stringByDeletingPathExtension;

stringByAppendingPathExtension

/** 拼接文件扩展名 */
- (nullable NSString *)stringByAppendingPathExtension:(NSString *)str;

stringByAbbreviatingWithTildeInPath

/** 绝对路径转为带~路径 */
@property (readonly, copy) NSString *stringByAbbreviatingWithTildeInPath;

stringByExpandingTildeInPath

/** 带~路径转为绝对路径 */
@property (readonly, copy) NSString *stringByExpandingTildeInPath;

stringByStandardizingPath

/** 标准化路径(会将~路径找出) */
@property (readonly, copy) NSString *stringByStandardizingPath;

stringByResolvingSymlinksInPath

/** 尝试解析路径中的符号链接 */
@property (readonly, copy) NSString *stringByResolvingSymlinksInPath;

stringsByAppendingPaths

/**
 *  将数组中的路径拼接到当前字符串之后,形成新的路径
 *  @param paths 路径数组(根据数组内容数量,拼接成相同数量的路径)
 *  @return 拼接后的路径保存在数组中返回
 */
- (NSArray<NSString *> *)stringsByAppendingPaths:(NSArray<NSString *> *)paths;

fileSystemRepresentation:maxLength:

/** 将一段NSString路径转化为C字符串形式 */
@property (readonly) __strong const char *fileSystemRepresentation;

/**
 将一段NSString路径转化为C字符串形式
 @param cname 用于接收C字符串的C字符串对象
 @param max 路径字符串的长度,必须比调用者的长度大
 @return 获取结果
 */
- (BOOL)getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max;

便利函数

/** 返回当前用户的登录名 */
NSString *NSUserName(void);

/** 返回当前用户的完整录名 */
NSString *NSFullUserName(void);

/** 返回应用的路径 */
NSString *NSHomeDirectory(void);

/** 返回指定登录名的应用路径 */
NSString * __nullable NSHomeDirectoryForUser(NSString * __nullable userName);

/** 返回当前应用Tep目录的路径 */
NSString *NSTemporaryDirectory(void);

/** 返回当前用户的系统根目录 */
NSString *NSOpenStepRootDirectory(void);

NSSearchPathDirectory

/** 重要目录的位置 */
typedef NS_ENUM(NSUInteger, NSSearchPathDirectory) {
    /** 支持的应用程序(/ Applications) */
    NSApplicationDirectory = 1,
    /** 不支持的应用程序,演示版本(~/Demos) */
    NSDemoApplicationDirectory,
    /** 开发应用程序(~Developer/Applications 弃用) */
    NSDeveloperApplicationDirectory,
    /** 系统和网络管理应用程序(~/Applications/Utilities) */
    NSAdminApplicationDirectory,
    /** 各种文档、支持和配置文件、资源(~/Library) */
    NSLibraryDirectory,
    /** 开发人员资源(Developer弃用) */
    NSDeveloperDirectory,
    /** 用户的主目录(Users) */
    NSUserDirectory,
    /** 文档(~/Library/Documentation) */
    NSDocumentationDirectory,
    /** 文档(~/Documents) */
    NSDocumentDirectory,
    /** CoreServices目录的位置(~/System/Library/CoreServices) */
    NSCoreServiceDirectory,
    /** 位置自动保存的文档(~/Documents/Autosaved) */
    NSAutosavedInformationDirectory = 11,
    /** 用户的桌面的位置(~/Desktop) */
    NSDesktopDirectory = 12,
    /** 缓存文件(~/Library/Caches) */
    NSCachesDirectory = 13,
    /** 插件 (~/Library/Application Support) */
    NSApplicationSupportDirectory = 14,
    /** 用户的“下载”目录(~/Downloads) */
    NSDownloadsDirectory = 15,
    /** 输入法(~/Library/Input Methods) */
    NSInputMethodsDirectory = 16,
    /** 用户的电影目录的位置(~/Movies) */
    NSMoviesDirectory  = 17,
    /** 用户的音乐目录的位置(~/Music) */
    NSMusicDirectory  = 18,
    /** 用户的图片目录的位置(~/Pictures) */
    NSPicturesDirectory  = 19,
    /** 打印机打印文件目录的位置 (~/Library/Printers/PPDs) */
    NSPrinterDescriptionDirectory = 20,
    /** 用户的公共共享目录的位置(~/Public) */
    NSSharedPublicDirectory = 21
    /** 偏好设置目录的位置(~/Library/PreferencePanes) */
    NSPreferencePanesDirectory  = 22,
    /** 脚本文件(~/Library/Application Scripts/code-signing-id) */
    NSApplicationScriptsDirectory = 23,
    /** 使用(NSFileManager's URLForDirectory:inDomain:appropriateForURL:create:error:)保存的目录 */
    NSItemReplacementDirectory = 99,
    /** 所有应用程序可能发生的目录 */
    NSAllApplicationsDirectory = 100,
    /** 所有可发生资源的目录 */
    NSAllLibrariesDirectory = 101,
    /** 所有垃圾文件的目录 */
    NSTrashDirectory = 102
};

NSSearchPathDomainMask

/** 域常量,指定搜索重要目录时要使用的基本位置。 */
typedef NS_OPTIONS(NSUInteger, NSSearchPathDomainMask) {
    /** 用户的home目录 */
    NSUserDomainMask = 1,
    /** 当前机器中 */
    NSLocalDomainMask = 2,
    /** 网络中可见的主机 */
    NSNetworkDomainMask = 4,
    /** 系统目录,不可修改 */
    NSSystemDomainMask = 8,
    /** 全部 */
    NSAllDomainsMask = 0x0ffff
};

eg

/** 例如获取沙盒library路径 */
[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值