Ios 文件路径中的可选值中存在展开错误
我有以下代码:Ios 文件路径中的可选值中存在展开错误,ios,swift,Ios,Swift,我有以下代码: func openPDFFromInternet(www: String){ let serwerUrl = ApiConstans.fullPath + "" + www let url = NSURL(string: www) print("the url = \(url)") UIApplication.shared.open(URL(string : www)!) } www有价值: 打印返回我零
func openPDFFromInternet(www: String){
let serwerUrl = ApiConstans.fullPath + "" + www
let url = NSURL(string: www)
print("the url = \(url)")
UIApplication.shared.open(URL(string : www)!)
}
www有价值:
打印返回我零
运行此代码时出现错误:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
有人知道修复它吗?我在这个路径中有一个文件。
我无法重命名我的文件名此问题是由于
URL
中包含无效字符(即空格)造成的。您需要对URL进行编码以删除无效字符
let localUrlString = "/Users/imac/Library/Developer/CoreSimulator/Devices/A2B19B93-A0AD-46DF-923F-E18DD76EAC96/data/Containers/Data/Application/A37F7B0E-3B67-42E8-B616-5C3066F5653F/Documents/enb\\g/PDF/MyFile RostiBites2015-ENG new_OK-prev.pdf"
if let localEncodedUrlString = localUrlString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) {
let localUrl = URL(fileURLWithPath: localEncodedUrlString)
}
你的函数名很容易引起误解,因为你问题中
www
的值显然是一个本地文件路径,所以要么更改函数名,要么如果它确实应该从远程URL打开文件,则将URL(fileURLWithPath:localEncodedUrlString)
更改为URL(字符串:localEncodedUrlString
并使用可选绑定安全地打开可失败的初始值设定项。该问题是由URL
中包含无效字符(即空格)引起的。您需要对URL进行编码以删除无效字符
let localUrlString = "/Users/imac/Library/Developer/CoreSimulator/Devices/A2B19B93-A0AD-46DF-923F-E18DD76EAC96/data/Containers/Data/Application/A37F7B0E-3B67-42E8-B616-5C3066F5653F/Documents/enb\\g/PDF/MyFile RostiBites2015-ENG new_OK-prev.pdf"
if let localEncodedUrlString = localUrlString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) {
let localUrl = URL(fileURLWithPath: localEncodedUrlString)
}
你的函数名很容易引起误解,因为你问题中
www
的值显然是一个本地文件路径,所以要么更改函数名,要么如果它确实应该从远程URL打开文件,则将URL(fileURLWithPath:localEncodedUrlString)
更改为URL(字符串:localEncodedUrlString
并使用可选绑定来安全地打开失败的初始值设定项。URL显然是一个文件系统URL–用前导斜杠表示–因此APIURL(字符串:
是错误的。您必须使用URL(fileURLWithPath
),其中百分比隐式转义空格字符
func openPDFFromInternet(www: String){
let serwerUrl = ApiConstans.fullPath + "" + www
let url = URL(fileURLWithPath : www)
print("the url = \(url)")
UIApplication.shared.open(url)
}
或者,如果URL可以是本地的或远程的,则使用更健壮的方法
func openPDFFromInternet(www: String){
let serwerUrl = ApiConstans.fullPath + "" + www
if www.hasPrefix("/") {
let url = URL(fileURLWithPath : www)
UIApplication.shared.open(url)
} else {
guard let escapedString = www.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed),
let url = URL(string : escapedString) else { return }
UIApplication.shared.open(url)
}
}
URL显然是一个文件系统URL–由一个前导斜杠表示–因此API
URL(字符串:
)是错误的。您必须使用URL(fileURLWithPath
,该百分比隐式转义空格字符
func openPDFFromInternet(www: String){
let serwerUrl = ApiConstans.fullPath + "" + www
let url = URL(fileURLWithPath : www)
print("the url = \(url)")
UIApplication.shared.open(url)
}
或者,如果URL可以是本地的或远程的,则使用更健壮的方法
func openPDFFromInternet(www: String){
let serwerUrl = ApiConstans.fullPath + "" + www
if www.hasPrefix("/") {
let url = URL(fileURLWithPath : www)
UIApplication.shared.open(url)
} else {
guard let escapedString = www.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed),
let url = URL(string : escapedString) else { return }
UIApplication.shared.open(url)
}
}
此处问题的可能重复是因为文件名中包含空格。删除文件名中的空格将返回URL值。同样,在另一个注释上,在“/PDF”之前使用了无效的转义序列“\g”,您没有收到警告,说明您没有使用常量serwerUrl吗?在第二行中也使用URL而不是NSURL如果您必须在文件名中有空格,您可以对文件名字符串进行URL编码,然后使用可能重复的问题,这是因为文件名中包含空格。删除文件名中的空格将返回URL值。同样,存在无效转义也是正确的在另一个注释中,在“/PDF”之前特别使用了“\g”序列,您是否收到警告,说明您没有使用常量serwerUrl?也可以在第二行中使用URL而不是NSURL如果您必须在文件名中有空格,您可以对文件名字符串进行URL编码,然后使用该代码正确启动,但不会打开PDF文件在默认系统程序中,这是另一个问题,请检查URL在运行时是否有效。是的,我有以下路径,例如:file:///Users/imac/Library/Developer/CoreSimulator/Devices/A2B19B93-A0AD-46DF-923F-E18DD76EAC96/data/Containers/Data/Application/E25C2467-1047-4C49-B8C4-75373D99ED1F/Documents/en/PDF/Folder%20A4-R2015-ENG%20new_OK-prev.pdf-当我复制此文件并粘贴到Firefox时-它会正确打开,没有任何错误此代码正确启动,但不会在默认系统程序中打开pdf文件实际上这是另一个问题,请检查URL在运行时是否有效。是的,我有此路径,例如:file:///Users/imac/Library/Developer/CoreSimulator/Devices/A2B19B93-A0AD-46DF-923F-E18DD76EAC96/data/Containers/data/Application/E25C2467-1047-4C49-B8C4-75373D99ED1F/Documents/en/PDF/文件夹%20A4-R2015-ENG%20new_OK-prev.PDF-当我复制并粘贴到Firefox时-它会正确打开,没有任何错误