我使用以下代码将文件保存在swift 3的文档目录中:
fileManager = FileManager.default
// let documentDirectory = fileManager?.urls(for: .documentDirectory, in: .userDomainMask).first as String
var path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
path = path + name
let image = #imageLiteral(resourceName: "Notifications")
let imageData = UIImageJPEGRepresentation(image, 0.5)
let bool = fileManager?.createFile(atPath: path, contents: imageData, attributes: nil)
print("bool is \(bool)")
return true
Run Code Online (Sandbox Code Playgroud)
但正如您所看到的,我不是filemanager
用来获取文档目录路径,因为filemanager
只提供URL而不是字符串.
问题: