Swift UIDocumentPickerViewController从iCloud上载txt文件
//参数标签“(contentsOfFile;,encoding;,erro:)”与任何可用的重载不匹配,上出现错误?字符串(内容文件:路径,编码:UTF8,错误:&错误)!:零Swift UIDocumentPickerViewController从iCloud上载txt文件,swift,upload,icloud,Swift,Upload,Icloud,//参数标签“(contentsOfFile;,encoding;,erro:)”与任何可用的重载不匹配,上出现错误?字符串(内容文件:路径,编码:UTF8,错误:&错误)!:零 有什么建议吗?如何从iCloud上传不适用于Swift 3的教程 替换openFile的函数体 @IBAction func iCloudPlayer(_ sender: Any) { var documentPicker = UIDocumentPickerViewController(documen
有什么建议吗?如何从iCloud上传不适用于Swift 3的教程 替换
openFile的函数体
@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
}
与
根据Swift 3.x的新变化,您是否查看了String
及其init
方法的Swift 3文档?您的问题与UIDocumentPickerViewController
或iCloud无关。@ArkadijsArchieMakarenko如果有效,请接受为勾号!
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
}
}