【发布时间】:2015-12-31 00:35:26
【问题描述】:
我想在 OSX 中将文件移动到另一个目录:
func moveFile(currentPath currentPath: String, targetPath: String) {
let fileManager = NSFileManager.defaultManager()
do { try fileManager.moveItemAtPath(currentPath, toPath: targetPath) }
catch let error as NSError { print(error.description) }
}
一切正常,除了目标目录不存在的情况。我发现.isWritableFileAtPath 可能会有所帮助。
但是,在我声明的函数中,我使用了完整的文件路径(包括文件名)。
如何从路径中拆分文件名或更一般地:如果需要,我如何强制 Swift 在移动文件之前创建目录?
【问题讨论】:
标签: macos cocoa swift2 nsfilemanager