不支持的URL Swift 5

我正在反序列化JSON是Swift5。我收到一条从未见过的奇怪错误消息。

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()
    }
}
yuyunc1 回答:不支持的URL Swift 5

没有协议类型(例如http,ftp等)的URL就不能拥有

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

“ file://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 }
,

替换

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

使用

guard let url = URL(string: "https://[[ENTER_THE_FULL_LINK_HERE_PLEASE]]\(urlString)") else { return }
本文链接:https://www.f2er.com/3096740.html

大家都在问