Friday, February 3, 2023
HomeiOS Developmentios - Can't parse mannequin json

ios – Can’t parse mannequin json


I attempt to parse JSON – https://madbearsclub.com/wp-json/wp/v2/posts.

I created subsequent mannequin:

struct Mannequin: Decodable {
    var title: Title
    var content material, excerpt: Content material
    var yoastHeadJSON: YoastHeadJSON
}

struct Content material: Decodable {
    var rendered: String
}

struct Title: Decodable {
    var rendered: String
}

struct YoastHeadJSON: Decodable {
    var ogImage: [OgImage]
}

struct OgImage: Decodable {
    var url: String
}

And use subsequent code to parse this mannequin:

static func getInfo(searh: String, web page: Int, completion: @escaping ([Model]) -> ()) {
        let urlToGet = "https://madbearsclub.com/wp-json/wp/v2/posts?search=(searh)&web page=(web page)"

        guard let urlString = URL(string: urlToGet) else {return}
        
        let fetch = URLSession.shared.dataTask(with: urlString) { (knowledge, responce, error) in
            
            guard let information = knowledge else {return}

            guard let present = attempt? JSONDecoder().decode([Model].self, from: information) else {return}
            
            completion(present)
        }
     fetch.resume()
    }

But it surely would not works. If I take away

var yoastHeadJSON: YoastHeadJSON

from mannequin it really works. However with it it would not work. The place is my error?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

one × three =

Most Popular

Recent Comments