不支持的 URL Swift 5

Dav*_*vid 2 json ios deserialization nsurlsession swift

我正在反序列化 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}
Run Code Online (Sandbox Code Playgroud)

这是我的 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\\\"]"
  }
]
Run Code Online (Sandbox Code Playgroud)

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

这是我的模型

struct Person: Codable {
  let image: String
  let filter: String
  let title: String
}
Run Code Online (Sandbox Code Playgroud)

这是我的扩展

// 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()
    }
}
Run Code Online (Sandbox Code Playgroud)

Joa*_*son 5

如果没有 http、ftp 等协议类型,就不可能有 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来构建正确的网址

var urlComponent = URLComponents()

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

guard let url = urlComponent.url else { return }
Run Code Online (Sandbox Code Playgroud)