iOS 沙盒文件重命名
2020-06-17 本文已影响0人
gaookey
func setupFileRename(_ filePath: String) -> String {
let url = URL(fileURLWithPath: filePath)
let newPath = filePath.replacingOccurrences(of: url.lastPathComponent, with: "")
let moveToPath = newPath + currentTime() + "." + url.pathExtension
do {
try FileManager.default.moveItem(at: url, to: URL(fileURLWithPath: moveToPath))
return moveToPath
} catch {
return ""
}
}
func currentTime() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMddHHmmss"
return formatter.string(from: Date())
}