文件不会写入磁盘。我究竟做错了什么?

时间:2015-02-25 15:40:35

标签: ios swift nsarray writetofile

我正在尝试将一组对象保存到文件中。那失败了。因此,为了测试我的过程,我尝试将一个简单的字符串写入文件,但也没有用。下面是我用来测试将字符串简单写入文件的代码。

    var directoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
    var path = directoryPath.stringByAppendingPathComponent(archivePath)
    var checkFile = NSFileManager.defaultManager()

    NSLog("Path: %@", path)

    var testString = "Hi my name is ken"
    var error : NSError?
    var written = testString.writeToFile(path, atomically: true, encoding: NSASCIIStringEncoding, error: &error)
    if ( written == false  ) {
        if ((error ) != nil) {
            var errString = error?.localizedDescription
            NSLog( "Fail: %@", errString! )
            var isDir : ObjCBool = false
            var isFile = checkFile.fileExistsAtPath(directoryPath, isDirectory: &isDir)
            if ( isDir ){
                NSLog("The directory %@, exists", directoryPath )
            } else {
                NSLog("The directory %@, does not exist", directoryPath )
            }

        }
    }

这是NSLog语句的结果:

2015-02-25 08:37:29.280 ... App Name ... [14596:3911337]路径:/ Users / cs / Library / Developer / CoreSimulator / Devices / 1747CFD7-5252-4221-AA89-BDEC1A57BA5A /data/Containers/Data/Application/5DC24B8F-A667-4ECD-B7E6-E139E65525E1/Documents/data/appdata.txt 2015-02-25 08:37:29.284 ... App Name ... [14596:3911337]失败:操作无法完成。 (可可错误4.) 2015-02-25 08:37:29.285 ... App Name ... [14596:3911337]目录/ Users / cs / Library / Developer / CoreSimulator / Devices / 1747CFD7-5252-4221-AA89-BDEC1A57BA5A / data /容器/数据/应用/ 5DC24B8F-A667-4ECD-B7E6-E139E65525E1 /文件,存在

3 个答案:

答案 0 :(得分:0)

您不包含存档路径,但我认为它是/data/appdata.txt。

文档目录存在,但除非您创建它,否则其中没有/ data文件夹。尝试将文件写入/data/appdata.txt不会自动创建数据目录。首先在.DocumentzDirectory中创建数据目录,或者只是尝试将文件保存到根目录(不带/ data /)。

答案 1 :(得分:0)

    var directoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
        var path = directoryPath.stringByAppendingPathComponent(archivePath)
        var checkFile = NSFileManager.defaultManager()

        NSLog("Path: %@", path)

        var testString = "Hi my name is ken"
        var error : NSError?
    //1. if your archivePath is just a file name with extension
        //remove exist file first
        var canWrite : bool = true
        if (checkFile.fileExistsAtPath(path)) {
           canWrite = checkFile.removeItemAtPath(path, error:&error)
        }

        if (!canWrite) {
           //show alert

           //and return
           return
        }
    //2. if your archivePath is a path with some folder, besure to create if folder does not exist and continue with //1.    

        var written = testString.writeToFil

e(path, atomically: true, encoding: NSASCIIStringEncoding, error: &error)
    if ( written == false  ) {
        if ((error ) != nil) {
            var errString = error?.localizedDescription
            NSLog( "Fail: %@", errString! )
            var isDir : ObjCBool = false
            var isFile = checkFile.fileExistsAtPath(directoryPath, isDirectory: &isDir)
            if ( isDir ){
                NSLog("The directory %@, exists", directoryPath )
            } else {
                NSLog("The directory %@, does not exist", directoryPath )
            }

        }
    }

答案 2 :(得分:0)

创建目录有帮助,但事实证明,问题的关键是我写入磁盘的数组包含的字典包含的值与plist不兼容。 Array.writeToFile ...写入Plists,如果数组中的数据与plist不兼容,则不会写入。我最终使用NSKeyArchiver和NSKeyUnArchiver来处理写作和阅读。一旦我使用它并处理了目录问题,这一切都奏效了。感谢大家的建议。