-1

Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “subash” in the folder “tmp”." UserInfo={NSFilePath=file:///private/var/mobile/Containers/Data/Application/902FE064-C3EC-42B5-A8F8-3D2923947067/tmp/subash, NSUnderlyingError=0x281e5c6f0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

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)
}
Yannick Loriot
  • 7,107
  • 2
  • 33
  • 56

2 Answers2

6

You are using the wrong API.

absoluteString is for remote URLs because the API will return also the URL scheme (e.g, http://, in this case file://).

To get a path from a file system URL you have to use path.

Nevertheless you are strongly discouraged from concatenating paths with +. Use always the URL related API and the dedicated path manipulation methods.

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)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
-1

try this one using objective 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);
}
Dani Eid
  • 104
  • 6