Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fileManager.createFileAtPath always fails

I try to call the fileManager.createFileAtPath-method, but it always fails. My variable success is always false. I looked at some similar posts here but nothing fits my needs exactly. Here is my code:

pListPath = NSURL(fileURLWithPath: String(reportsPath)).URLByAppendingPathComponent("myReports.plist", isDirectory: false)

                let data: NSData = NSData()
                var isDir: ObjCBool = false

            if fileManager.fileExistsAtPath(String(pListPath), isDirectory: &isDir)
                {
                    print("File already exists")
                }
                else
                {

                    let success = fileManager.createFileAtPath(String(pListPath), contents: data, attributes: nil)

                    print("Was file created?: \(success)")
                    print("plistPath: \(pListPath)")
                }

Here is how i tried to fix this:

I tried to use the method

let success = NSFileManager.defaultManager().createFileAtPath(String(pListPath), contents: data, attributes: nil)

And also

let success = data.writeToFile(String(pListPath), atomically: true)

But nothing works. successis always false. I also tried to give it a literal-String as a path, set contentsto nil and I even changed the directory permissions where i want to put it to 777, but noting works. successis always false. I hope you can me help out of this. Any help is highly appreciate. Thank you

like image 325
user1895268 Avatar asked Jan 28 '16 18:01

user1895268


2 Answers

Here is a problem:

if fileManager.fileExistsAtPath(String(pListPath), isDirectory: &isDir)

You cannot use String(...) to convert a NSURL to a file path string, you have to use the path method:

if fileManager.fileExistsAtPath(pListPath.path!, isDirectory: &isDir)

If reportsPath is also an NSURL then the same problem exists at

pListPath = NSURL(fileURLWithPath: String(reportsPath)).URLByAppendingPathComponent("myReports.plist", isDirectory: false)

which should be

let pListPath = reportsPath.URLByAppendingPathComponent("myReports.plist", isDirectory: false)
like image 169
Martin R Avatar answered Nov 20 '22 13:11

Martin R


Make sure that you try to create file in folder that already exists. First create folder, then you can create file at that path.

private class func assureDirPathExists(path: String)
{
    if !NSFileManager.defaultManager().fileExistsAtPath(path)
    {
        do {
            try NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: false, attributes: nil)
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    }
}

Make sure you are writing file relatively to user domain folder.

 // i.e. Caches, Documents...
 let rootPath = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true) 

Probably, you are missing the slash character, or you skip some folder creation first.

If you have ...dir1/dir2/file.ext than you need to create dir1, then dir2 and finally file.ext.

like image 8
Krešimir Prcela Avatar answered Nov 20 '22 13:11

Krešimir Prcela