cocoa path operation

  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);

你可能感兴趣的:(cocoa path operation)