我在文档目录中有一个名为a.caf的单个文件,我想在用户在
我该如何做?
UITextField
中输入并按下更改时将其重命名(UITextField
中输入的文本应成为新文件名)。我该如何做?
UITextField
中输入并按下更改时将其重命名(UITextField
中输入的文本应成为新文件名)。你可以使用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];
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)
}
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
}
}