1

I am trying to create a custom directory using following snippet, that I essentially translated from a working Obj-C code for the same app.

class func pathToConfigFolder() -> String {
    let urls: [String] = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)
    let libraryPath: String = urls.last!
    let configFolder: String = NSURL(fileURLWithPath: libraryPath).URLByAppendingPathComponent(".conf").absoluteString

    var directory: ObjCBool = ObjCBool(true)
    if !NSFileManager.defaultManager().fileExistsAtPath(configFolder, isDirectory: &directory) {
        do {
            try NSFileManager.defaultManager().createDirectoryAtPath(configFolder, withIntermediateDirectories: false, attributes: nil)
        } catch let error {
            print(" Error thrown... \(error)")
        }
    }
    return configFolder
}

But the code fails with following error.

Error thrown... Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x17526f4c0 {NSFilePath=file:///var/mobile/Containers/Data/Application/44D90AEB-DD7A-4C8C-9AD0-2665147BAAEC/Library/conf, NSUnderlyingError=0x174054c40 "The operation couldn’t be completed. No such file or directory"}

I have tried both on device and simulator running iOS 9.

EDIT Objective-C Code

+ (NSString*) pathToConfigFolder
{
    NSArray *urls = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
                                                        NSUserDomainMask, YES);
    NSString *libraryPath = [urls lastObject];

    NSString *configFolder = [libraryPath stringByAppendingPathComponent:@".conf"];

    NSError *error = nil;

    if ( ! [[NSFileManager defaultManager] fileExistsAtPath:configFolder]) {

        [[NSFileManager defaultManager] createDirectoryAtPath:configFolder
                                  withIntermediateDirectories:NO
                                                   attributes:nil
                                                        error:&error];
    }
    return configFolder;
}
1
  • Attach the Obj-C code, maybe we can spot the differences. Commented Jul 20, 2016 at 21:54

1 Answer 1

1

Your problem is here:

let libraryPath: String = urls.last!
let configFolder: String = NSURL(fileURLWithPath: libraryPath).URLByAppendingPathComponent(".conf").absoluteString

The absoluteString method returns the entire URL as a string, so your configFolder is file:///var/mobile/Containers/Data/Application/44D90AEB-DD7A-4C8C-9AD0-2665147BAAEC/Library/conf. That is, it includes the file:// and is therefore not a valid file path suitable for use with the NSFileManager methods you use it with.

Changing those lines to look like this would fix the problem:

let libraryPath: NSString = urls.last!
let configFolder = libraryPath.stringByAppendingPathComponent(".conf")

I'm using NSString here because that's where stringByAppendingPathComponent is defined.

Sign up to request clarification or add additional context in comments.

4 Comments

When I used String the compiler complained that stringByAppendingPathComponent didn't exist.
The code I posted does not use URLs, it uses file paths. I left the misnamed urls variable alone because changing it didn't seem relevant.
@LeoDabus please do feel free to write your own answer to this question.
thank you so much, it did the trick. Had to cast String to NSString to use that method. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.