2015-12-23 19 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)") 
     } 
    }