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)")
}
}
}
^崩溃步骤方法:FileManager.default.createFile()
^只需运行app,不要调试,干得好