2015-12-23 23 views
0

我有一个文件,它是在我的Documents/Inbox,在我Print日志显示:移动文件/收件箱文件到另一个位置 - Xcode的7,斯威夫特2

文件: 文件:///私人/无功/移动/集装箱/数据/应用/ 5388031B-48B5-48D6-8299-B3FEDC1D7F45 /文档/收件箱/比萨,6.pdf

我看着here,看到的方式来删除文件,但我想将它们移出Inbox文件夹到我想要创建的另一个文件夹。我将如何做到这一点?我无法找到iOS和Swift 2的任何内容。谢谢。

+0

“的Xcode的7”? Xcode是一个IDE。你的意思是使用Swift的iOS吗?无论如何,我期望它会涉及'NSFileManager',所以从搜索“iOS移动文件”和“nsfilemanager移动文件”开始。然后您需要将解决方案从Objective-C转换为Swift。 – trojanfoe

+0

谢谢。我编辑了我的信息。我是编程新手,所以试图将Objective-C转换为Swift对我来说很难。我会看看我能做什么。 – ChallengerGuy

回答

0

这里是我落得这样做:

// MOVING AND SAVING INCOMING PDF TO FILE MANAGER FROM INBOX 

    let filemgr = NSFileManager.defaultManager() 
    let docsDirURL = try! filemgr.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) 

    // Create a new folder in the directory named "Recipes" 
    print("Creating new folder...") 
    let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]) 
    let newPath = documentsPath.URLByAppendingPathComponent("Recipes") 
    do { 
     try NSFileManager.defaultManager().createDirectoryAtPath(newPath.path!, withIntermediateDirectories: true, attributes: nil) 
    } catch let error as NSError { 
     NSLog("Unable to create directory \(error.debugDescription)") 
    } 

    // Then check if the Recipes directory exists. If not, create it 
    let recipesURL = docsDirURL.URLByAppendingPathComponent("Recipes") 
    if !filemgr.fileExistsAtPath(docsDirURL.path!) { 
     do { 
      try filemgr.createDirectoryAtURL(recipesURL, withIntermediateDirectories: true, attributes: nil) 
      print("Directory created at: \(recipesURL)") 
     } catch let error as NSError { 
      NSLog("Unable to create directory \(error.debugDescription)") 
      return 
     } 
    } 

    // Move file from Inbox to Recipes Folder 
    let incomingFileName = incomingFileTransfer.lastPathComponent! 
    let startingURL = incomingFileTransfer 
    let savePDFURL = recipesURL.URLByAppendingPathComponent(incomingFileName) 

    if !filemgr.fileExistsAtPath(savePDFURL.path!) { 
     do { 
      try filemgr.moveItemAtURL(startingURL, toURL: savePDFURL) 
     } catch let error as NSError { 
      NSLog("Unable to move file \(error.debugDescription)") 
     } 
    }