我正在尝试从documentPicker获取文件类型。我允许我的用户上传两种PDF / WORD(doc / docx)文档类型,这对我至关重要,因此我可以重命名该文件并上传到我的AWS S3。
感谢任何帮助,我找不到直接从UIDocumentPickerViewController中获取它的方法。
因此,我尝试查找整个URL的长度,然后将长度减去2/3,以找到第一个字母“ d”或“ p”,然后可以相应地设置名称。但是,我收到此错误,无法使用类型为(URL)的参数列表来调用类型为String的初始化程序。
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let myURL = url as URL
print("import result : \(myURL)")
let s3BucketName = "adnexio-directory/cv_upload"
let url = myURL
var remoteName = ""
//if (url[url.length as Int - 3] == "d")
//if (str[str.count - 3] == "d")
if (url.length - 3 == "d")
{
remoteName = "IOSTEST.docx"
}
else if (url.length - 2 == "d")
{
remoteName = "IOSTEST.doc"
}
else
{
remoteName = "IOSTEST.pdf"
}
print("REMOTE NAME : ",remoteName)
答案 0 :(得分:2)
最简单的方法是检查URL的路径扩展
let extension = myURL.pathExtension
switch extension {
case "pdf" : print("It's PDF")
case "doc", "docx" : print("It's a MS Word document")
default : print("It's unknown")
}
答案 1 :(得分:-2)
我解决了。我添加了以下有关如何解决此问题的信息:
let str = url.absoluteString
if str[str.count - 4] == "d"
{
remoteName = "IOSTEST.docx"
}
else if str[str.count - 3] == "d"
{
remoteName = "IOSTEST.doc"
}
else
{
remoteName = "IOSTEST.pdf"
}
,您将需要添加此扩展名
extension String {
var length: Int {
return count
}
subscript (i: Int) -> String {
return self[i ..< i + 1]
}
func substring(fromIndex: Int) -> String {
return self[min(fromIndex, length) ..< length]
}
func substring(toIndex: Int) -> String {
return self[0 ..< max(0, toIndex)]
}
subscript (r: Range<Int>) -> String {
let range = Range(uncheckedBounds: (lower: max(0, min(length, r.lowerBound)),
upper: min(length, max(0, r.upperBound))))
let start = index(startIndex, offsetBy: range.lowerBound)
let end = index(start, offsetBy: range.upperBound - range.lowerBound)
return String(self[start ..< end])
}
}
我不知道为什么默认值中还没有这个。