atastrophic
atastrophic

Reputation: 3143

Creating custom directory in iOS Swift 2.2

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

Upvotes: 1

Views: 102

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70936

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.

Upvotes: 1

Related Questions