创建文件,逐步调试,崩溃 - EXC_BAD_ACCESS

时间:2017-12-19 04:19:56

标签: ios exc-bad-access

  • 我想创建test.txt文件,示例代码。
  • 我运行这个应用程序,做得很好。
  • 但是当我逐步调试应用程序时,它会崩溃,报告 (EXC_BAD_ACCESS(代码= EXC_l386_GPFLT)),方法: FileManager.default.createFile()

SWIFT CODES:

import UIKit

    class ViewController: UIViewController {

        @IBAction func CreateFileButton(_ sender: UIButton) {
            createFiles()
        }

        private func createFiles() {
            let manager = FileManager.default
            let urlForDocument = manager.urls( for: .documentDirectory,
                                               in:.userDomainMask)
            let url = urlForDocument[0]
            createFile(name:"test.txt", fileBaseUrl: url)
        }
        func createFile(name:String, fileBaseUrl:URL){
            let manager = FileManager.default

            let file = fileBaseUrl.appendingPathComponent(name)
            print("f: \(file)")
            let exist = manager.fileExists(atPath: file.path)
            if !exist {
                let data = Data(base64Encoded:"aGVsbG8gd29ybGQ=" ,options:.ignoreUnknownCharacters)
                let createSuccess = manager.createFile(atPath: file.path,contents:data,attributes:nil)
                print("f: \(createSuccess)")
            }
        }
    }

enter image description here

^崩溃步骤方法:FileManager.default.createFile() enter image description here

^崩溃报告 enter image description here

^只需运行app,不要调试,干得好

0 个答案:

没有答案