我在文档目录中有一个名为a.caf的文件。我想在用户输入 aUITextField
并按下 change 时重命名它(输入的文本UITextField
应该是新的文件名)。
我怎样才能做到这一点?
我在文档目录中有一个名为a.caf的文件。我想在用户输入 aUITextField
并按下 change 时重命名它(输入的文本UITextField
应该是新的文件名)。
我怎样才能做到这一点?
You can use moveItemAtPath.
NSError * err = NULL;
NSFileManager * fm = [[NSFileManager alloc] init];
BOOL result = [fm moveItemAtPath:@"/tmp/test.tt" toPath:@"/tmp/dstpath.tt" error:&err];
if(!result)
NSLog(@"Error: %@", err);
[fm release];
为了使这个问题保持最新,我还添加了Swift版本:
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
let originPath = documentDirectory.stringByAppendingPathComponent("/tmp/a.caf")
let destinationPath = documentDirectory.stringByAppendingPathComponent("/tmp/xyz.caf")
var moveError: NSError?
if !manager.moveItemAtPath(originPath, toPath: destinationPath, error: &moveError) {
println(moveError!.localizedDescription)
}
这是 daehan park 转换为 Swift 3 的功能:
func moveFile(pre: String, move: String) -> Bool {
do {
try FileManager.default.moveItem(atPath: pre, toPath: move)
return true
} catch {
return false
}
}
在Swift 2.2上工作
func moveFile(pre: String, move: String) -> Bool {
do {
try NSFileManager.defaultManager().moveItemAtPath(pre, toPath: move)
return true
} catch {
return false
}
}