2015-12-23 1 views
0

У меня есть файл, который находится в моем Documents/Inbox и отображается в моем журнале Print:Переместить документ/файл Входящие в другое место - Xcode 7, Swift 2

Файл: файла: /// частный/вар/мобильный/Контейнеры/Data/Application/5388031B-48B5-48D6-8299-B3FEDC1D7F45/Документы/Входящие/Пицца-6.pdf

Я посмотрел here и увидел способ удаления файлов, но я хочу переместите их из папки Inbox в другую папку, которую я хочу создать. Как мне это сделать? Я ничего не могу найти для iOS и Swift 2. Спасибо.

+0

"для Xcode 7"? Xcode - это IDE. Вы имели в виду iOS, используя Swift? В любом случае, я ожидаю, что он будет включать '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)") 
     } 
    } 
Смежные вопросы