我无法在文件管理器中创建目录

时间:2019-01-28 09:34:42

标签: ios swift

  

Error Domain = NSCocoaErrorDomain代码= 513“您无权将文件“ subash”保存在“ tmp”文件夹中。” UserInfo = {NSFilePath = file:/// private / var / mobile / Containers / Data / Application / 902FE064-C3EC-42B5-A8F8-3D2923947067 / tmp / subash,NSUnderlyingError = 0x281e5c6f0 {Error Domain = NSPOSIXErrorDomain Code = 1“允许“}}

do {
  var mytmppath:String=FileManager.default.temporaryDirectory.absoluteString+"subash"
  try FileManager.default.createDirectory(atPath: mytmppath, withIntermediateDirectories: true, attributes: nil)

  print( FileManager.default.subpaths(atPath: FileManager.default.temporaryDirectory.absoluteString))
} catch {
  print(error)
}

2 个答案:

答案 0 :(得分:0)

您使用了错误的API。

absoluteString用于远程URL,因为API还将返回URL方案(例如http://,在这种情况下为file://)。

要从文件系统URL获取路径,必须使用path

尽管如此,强烈建议您不要将路径与+串联在一起。始终使用与URL相关的API和专用的路径操作方法。

do {
    let defaultManager = FileManager.default
    let temporarySubURL = defaultManager.temporaryDirectory.appendingPathComponent("subash")
    try defaultManager.createDirectory(at: temporarySubURL, withIntermediateDirectories: true, attributes: nil)

    print( defaultManager.subpaths(atPath: FileManager.default.temporaryDirectory.path))
} catch {
    print(error)
}

答案 1 :(得分:0)

使用目标c尝试这个

e NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
documentsPath = [documentsPath stringByAppendingPathComponent:currentUser.mobileNumber];
NSString *filePathAndDirectory = [documentsPath stringByAppendingPathComponent:directoryName];
NSError *error;

if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
                               withIntermediateDirectories:YES
                                                attributes:nil
                                                     error:&error])
{
    NSLog(@"Create directory error: %@", error);
}