如何在Cocoa中创建新文件夹?

时间:2011-03-26 17:25:00

标签: objective-c cocoa macos

我想知道如何在某个目录中创建新文件夹。我该怎么做?我使用NSFileManager

2 个答案:

答案 0 :(得分:5)

是的,您可以使用NSFileManager的createDirectoryAtPath:withIntermediateDirectories:attributes:error:方法:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html

[ [ NSFileManager defaultManager ] createDirectoryAtPath: @"/My/Custom/Path" withIntermediateDirectories: YES attributes: nil error: NULL ];

答案 1 :(得分:0)

Swift 5

  let path = "/Users/\(NSUserName())/Downloads/custom"
   do {
       try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
   } catch{
       print(error)
  }