gpt4 book ai didi

ios - 不支持的 URL Swift 5

转载 作者:行者123 更新时间:2023-12-02 19:48:05 26 4
gpt4 key购买 nike

我正在反序列化 JSON is Swift 5。我收到一条以前从未见过的奇怪错误消息。

2019-11-15 00:53:59.246948-0800 WW-Exercise-01[14071:7505805] Task <8291D72C-ACB0-4C9D-8169-60C8D38E6555>.<3> finished with error - code: -1002
2019-11-15 00:53:59.267601-0800 WW-Exercise-01[14071:7505806] Error fetching data Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x6000002557b0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=/images/1033/dynamic/foodandrecipes/2015/03/MA15_Chili_Vegetarian_0104_rt_800x800.jpg, NSErrorFailingURLKey=/images/1033/dynamic/foodandrecipes/2015/03/MA15_Chili_Vegetarian_0104_rt_800x800.jpg, NSLocalizedDescription=unsupported URL}
2019-11-15 00:53:59.275585-0800 WW-Exercise-01[14071:7505805] Task <10DCC226-2F4F-4BF4-884E-B9878959D6A5>.<4> finished with error - code: -1002
2019-11-15 00:53:59.276525-0800 WW-Exercise-01[14071:7505806] Error fetching data Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x604000255690 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=/images/1033/dynamic/foodandrecipes/2014/06/FetaStuffedCherryTomatoes_267_600.jpg, NSErrorFailingURLKey=/images/1033/dynamic/foodandrecipes/2014/06/FetaStuffedCherryTomatoes_267_600.jpg, NSLocalizedDescription=unsupported URL}
2019-11-15 00:53:59.458865-0800 WW-Exercise-01[14071:7505803] Task <F36DF3CF-529F-461B-AF84-D6C07C1A1A36>.<5> finished with error - code: -1002
2019-11-15 00:53:59.459230-0800 WW-Exercise-01[14071:7505806] Error fetching data Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x604000255120 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=/images/1033/dynamic/foodandrecipes/2011/12/HCO_LemonDillGreekChickenSalad_178_600.jpg, NSErrorFailingURLKey=/images/1033/dynamic/foodandrecipes/2011/12/HCO_LemonDillGreekChickenSalad_178_600.jpg, NSLocalizedDescription=unsupported URL}
2019-11-15 00:53:59.477245-0800 WW-Exercise-01[14071:7505804] Task <B3F9DD43-4AD6-4D4F-BD49-679C5CA59D05>.<6> finished with error - code: -1002
2019-11-15 00:53:59.478623-0800 WW-Exercise-01[14071:7505806] Error fetching data Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x604000255e70 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=/images/1033/dynamic/foodandrecipes/2015/08/TurkeyTacoCups_259_600x600.jpg, NSErrorFailingURLKey=/images/1033/dynamic/foodandrecipes/2015/08/TurkeyTacoCups_259_600x600.jpg, NSLocalizedDescription=unsupported URL}
2019-11-15 00:53:59.727535-0800 WW-Exercise-01[14071:7505803] Task <99D0917B-BED2-4728-A6A0-BC94B50809A0>.<7> finished with error - code: -1002
2019-11-15 00:53:59.728287-0800 WW-Exercise-01[14071:7505806] Error fetching data Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSUnderlyingError=0x604000254280 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=/images/1033/dynamic/foodandrecipes/2015/08/LemonyBeetHummus_052_800x800.jpg, NSErrorFailingURLKey=/images/1033/dynamic/foodandrecipes/2015/08/LemonyBeetHummus_052_800x800.jpg, NSLocalizedDescription=unsupported URL}

这是我的 JSON

[
{
"image": "/images/1033/dynamic/foodandrecipes/2015/07/TANDORI_GRILLED_SHRIMP_1053_800x800.jpg",
"filter": "[\\\"contentTags.food_cooking method.tags:Grilling\\\"]",
"title": "Summer Grilling"
},
{
"image": "/images/1033/dynamic/foodandrecipes/2013/09/MediterraneanBeanSalad_1187_600.jpg",
"title": "Latest",
"filter": ""
},
{
"filter": "[\\\"contentTags.food_other.tags:Quick & Easy\\\"]",
"image": "/images/1033/dynamic/foodandrecipes/2014/07/BeanCheeseHotDogs_052_600.jpg",
"title": "Quick & Easy"
},
{
"title": "No-Cook",
"image": "/images/1033/dynamic/foodandrecipes/2015/08/LemonyBeetHummus_052_800x800.jpg",
"filter": "[\\\"contentTags.food_cooking method.tags:No-Cook\\\"]"
},
{
"title": "Family-Friendly",
"filter": "[\\\"contentTags.food_other.tags:Family-Friendly\\\"]",
"image": "/images/1033/dynamic/foodandrecipes/2015/08/TurkeyTacoCups_259_600x600.jpg"
},
{
"filter": "[\\\"contentTags.food_season.tags:Summer\\\"], [\\\"contentTags.food_season.tags:Spring\\\"]",
"title": "Seasonal",
"image": "/images/1033/dynamic/foodandrecipes/2011/12/HCO_LemonDillGreekChickenSalad_178_600.jpg"
},
{
"filter": "[\\\"contentTags.food_cuisines.tags:American\\\"]",
"image": "/images/1033/dynamic/foodandrecipes/2015/03/MA15_Chili_Vegetarian_0104_rt_800x800.jpg",
"title": "American"
},
{
"title": "Italian",
"image": "/images/1033/dynamic/foodandrecipes/2014/06/FetaStuffedCherryTomatoes_267_600.jpg",
"filter": "[\\\"contentTags.food_cuisines.tags:Italian\\\"]"
}
]

我错过了什么?我的 JSON 我有效。我取回了 titlefilter 但没有取回图像。

这是我的模型

struct Person: Codable {
let image: String
let filter: String
let title: String
}

这是我的扩展

// MARK: - Local cache for saving images in memory

let imageCache = NSCache<NSString, UIImage>()

extension UIImageView {
func loadImageUsingCache(with urlString: String) {

self.image = nil

// check for cached image

if let cachedImage = imageCache.object(forKey: urlString as NSString) {
self.image = cachedImage
return
}

// otherwise download image

guard let url = URL(string: urlString) else { return }

URLSession.shared.dataTask(with: url) { (data, _, error) in
if let error = error {
NSLog("Error fetching data \(error)")
}
guard let data = data else { return }

DispatchQueue.main.async {
if let downloadedImage = UIImage(data: data) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
}
}.resume()
}
}

最佳答案

如果没有 http、ftp 等协议(protocol)类型,就无法拥有 URL

因此,如果您的图像存储在本地,则应该类似于

“文件://images/1033/dynamic/foodandrecipes/2015/07/TANDORI_GRILLED_SHRIMP_1053_800x800.jpg”

但如果它们位于服务器上,那么您不仅会丢失 http(s) 部分,还会丢失整个服务器地址

"http://127.0.0.1/images/1033/dynamic/foodandrecipes/2015/07/TANDORI_GRILLED_SHRIMP_1053_800x800.jpg "

更新

对于您的扩展,我建议使用 URLComponents 类来构建正确的 url

var urlComponent = URLComponents()

urlComponent.scheme = "https"
urlComponent.host = "www.weightwatchers.com"
urlComponent.path = urlString

guard let url = urlComponent.url else { return }

关于ios - 不支持的 URL Swift 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com