2016-11-28 112 views
-2
@IBAction func iCloudPlayer(_ sender: Any) { 
     var documentPicker = UIDocumentPickerViewController(documentTypes: ["public.txt"], in: UIDocumentPickerMode.import) 
     documentPicker.delegate = self 
     documentPicker.modalPresentationStyle = UIModalPresentationStyle.fullScreen 
     self.present(documentPicker, animated: true, completion: nil) 
    } 

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL){ 
     if(controller.documentPickerMode == UIDocumentPickerMode.import){ 
      let content = openFile(path: url.path, UTF8: String.Encoding.utf8) 
      titlePlayerContent.text = content 
     } 
    } 
    func openFile(path:String, UTF8:String.Encoding = String.Encoding.utf8) -> String?{ 
     var error: NSError? 
     return FileManager().fileExists(atPath: path) ? String(contentsOfFile: path, encoding: UTF8, error: &error)! : nil 

    } 

//Arguments labels '(contentsOfFile;,encoding;, erro:)' do not match any available overloads, error on ? String(contentsOfFile: path, encoding: UTF8, error: &error)! : nilUIDocumentPickerViewController從iCloud中

任何建議傢伙上傳txt文件?我如何從iCloud上傳,不能在Swift 3上使用的教程!

+0

你有沒有看着斯威夫特3文檔'字符串'和它的'init'方法? – rmaddy

+0

而你的問題與'UIDocumentPickerViewController'或iCloud無關。 – rmaddy

回答

1

更換的openFile

func openFile(path:String, UTF8:String.Encoding = String.Encoding.utf8) -> String?{ 
     var error: NSError? 
     return FileManager().fileExists(atPath: path) ? String(contentsOfFile: path, encoding: UTF8, error: &error)! : nil 

    } 

你的函數體

func openFile(path:String, UTF8:String.Encoding = String.Encoding.utf8) -> String?{ 
     if FileManager().fileExists(atPath: path) { 
      do { 
       let string = try String(contentsOfFile: path, encoding: .utf8) 
       return string 
      }catch let error as NSError{ 

       //Handle your error/exception here. I just returned a error as a string. You can return nil or something in this case too. 
       return error.localizedDescription 
      } 
     } else { 
      return nil 
     } 
    } 

按照新變化斯威夫特3.X

+0

謝謝,它的作品!好的破解 –

+0

@ArkadijsArchieMakarenko如果有效,請親切接受! –

相關問題