UIDocumentPickerViewController从iCloud

时间:2016-11-28 00:07:55

标签: swift upload icloud

@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

    }
  

//参数标签'(contentsOfFile;,encoding;,erro:)'不匹配任何可用的重载,错误? String(contentsOfFile:path,encoding:UTF8,error:& error)! :没有了

任何建议的家伙?我如何从iCloud上传,教程不适用于Swift 3!

1 个答案:

答案 0 :(得分: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
        }
    }

根据 Swift 3.x

中的新变化