cocoa path operation

本文介绍如何使用Swift语言进行路径操作,包括判断路径是否为文件夹及枚举文件夹中的文件等实用技巧。此外还提供了获取用户名、家目录等常用路径的方法。

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

  1. Path operation
  2. 1>.判断路径是否为文件夹:
  3. NSArray *subpaths;
  4. BOOL isDir;
  5. NSArray *paths = NSSearchPathForDirectoriesInDomains
  6.                      (NSLibraryDirectory, NSUserDomainMask, YES);
  7. if ([paths count] == 1)
  8. {
  9.     NSFileManager *fileManager = [NSFileManager defaultManager];
  10.     NSString *fontPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Fonts"];
  11.     if ([fileManager fileExistsAtPath:fontPath isDirectory:&isDir] && isDir)
  12.     {
  13.         subpaths = [fileManager subpathsAtPath:fontPath];
  14.     }
  15. }
  16. 2>.枚举文件夹中的文件
  17. NSString *file;
  18. NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:  @"Documents"];
  19. NSDirectoryEnumerator *dirEnum =[[NSFileManager defaultManager] enumeratorAtPath:docsDir];
  20. while (file = [dirEnum nextObject])
  21. {
  22.     if ([[file pathExtension] isEqualToString: @"doc"])
  23.     {
  24.         [self scanDocument: [docsDir stringByAppendingPathComponent:file]];
  25.     }
  26. }

其他的方法可以参考 NSPathUtilities.h

FOUNDATION_EXPORT NSString *NSUserName(void);
FOUNDATION_EXPORT NSString *NSFullUserName(void);

FOUNDATION_EXPORT NSString *NSHomeDirectory(void);
FOUNDATION_EXPORT NSString *NSHomeDirectoryForUser(NSString *userName);

FOUNDATION_EXPORT NSString *NSTemporaryDirectory(void);

FOUNDATION_EXPORT NSString *NSOpenStepRootDirectory(void);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值