How to rename a file using NSFileManager

后端 未结 4 1676
[愿得一人]
[愿得一人] 2020-12-24 05:51

I have a single file named a.caf in the documents directory. I would like to rename it when user types into a UITextField and presses change (t

4条回答
  •  北海茫月
    2020-12-24 06:17

    To keep this question up-to-date, I'm adding the Swift version as well:

    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)
    }
    

提交回复
热议问题