2016-08-01 160 views
-1

Xcode 7.3.1,Swift 2.2重命名文件而不输入文件扩展名

我下面的代码是按预期重命名文件。我的问题是,我想要在警报文本输入字段中输入没有扩展名的“新文件名”。然后,一旦用户按下“是”,文件扩展名“m4u”被添加到新名称并最终重命名文件“新文件名.m4u”。我不希望用户必须处理文件扩展名。

任何援助将不胜感激.....

func askToRename(row:Int) { 

    let recording = self.arrayRecordings[row] 
    let recordingURL = self.arrayRecordingsURL[row] 

    let alert = UIAlertController(title: "Rename", 
            message: "Rename Recording \(recording)?", 
            preferredStyle: .Alert) 
    alert.addAction(UIAlertAction(title: "Yes", style: .Default, handler: {[unowned alert] action in 
     print("yes was tapped \(self.arrayRecordingsURL[row])") 
     if let textFields = alert.textFields{ 
      let tfa = textFields as [UITextField] 
      let text = tfa[0].text 
      let url = NSURL(fileURLWithPath: text!) 
      self.renameRecording(recordingURL, to: url) 
     } 
     })) 
    alert.addAction(UIAlertAction(title: "No", style: .Default, handler: {action in 
     print("no was tapped") 
    })) 
    alert.addTextFieldWithConfigurationHandler({textfield in 
     textfield.placeholder = "Enter a filename" 
     textfield.text = "\(recordingURL.lastPathComponent!)" 
    }) 
    self.presentViewController(alert, animated:true, completion:nil) 
} 


func renameRecording(from:NSURL, to:NSURL) { 
    let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] 
    let toURL = documentsDirectory.URLByAppendingPathComponent(to.lastPathComponent!) 

    print("renaming file \(from.absoluteString) to \(to) url \(toURL)") 
    let fileManager = NSFileManager.defaultManager() 
    fileManager.delegate = self 
    do { 
     try NSFileManager.defaultManager().moveItemAtURL(from, toURL: toURL) 
    } catch let error as NSError { 
     print(error.localizedDescription) 
    } catch { 
     print("error renaming recording") 
    } 
    dispatch_async(dispatch_get_main_queue(), { 
     self.listRecordings() 
     //   self.tableView.reloadData() 
    }) 

} 


func listRecordings() { 

    let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] 
    do { 
     let urls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsDirectory, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles) 
     self.arrayRecordingsURL = urls.filter({ (name: NSURL) -> Bool in 
      return name.lastPathComponent!.hasSuffix("m4a") 
     }) 

    } catch let error as NSError { 
     print(error.localizedDescription) 
    } catch { 
     print("something went wrong listing recordings") 
    } 

} 



extension ViewControllerFileManager: NSFileManagerDelegate { 

func fileManager(fileManager: NSFileManager, shouldMoveItemAtURL srcURL: NSURL, toURL dstURL: NSURL) -> Bool { 

    print("should move \(srcURL) to \(dstURL)") 
    return true 
} 

}

+1

如何添加在你的代码路径扩展。 // let text = tfa [0] .text +“.m4u” – WeiJay

+0

感谢Wei Jay的帮助,不幸的是没有做到这一点。 – Bill

回答

1

我能够使一对夫妇的小teaks我原来上面的代码,以实现所需的功能..

在“askToRename”函数中,我修改了以下几行。

来源:

let url = NSURL(fileURLWithPath: text!) 

要:

let url = NSURL(fileURLWithPath: text!).URLByAppendingPathExtension("m4a") 

来源:

textfield.text = "\(recordingURL.lastPathComponent!)" 

要:

textfield.text = "\(recordingURL.URLByDeletingPathExtension!.lastPathComponent!)"