Friday, November 14, 2025
HomeiOS Developmentios - worth of kind 'Any' has no subscripts

ios – worth of kind ‘Any’ has no subscripts

[ad_1]

I am studying to make use of APIs and this API that I need to use has completely different information sorts.

{
"d":[
{
"i":{
"height":1500
"imageUrl":"https://m.media-amazon.com/images/M/MV5BYTRiNDQwYzAtMzVlZS00NTI5LWJjYjUtMzkwNTUzMWMxZTllXkEyXkFqcGdeQXVyNDIzMzcwNjc@._V1_.jpg"
"width":1102
}
"id":"tt0944947"
"l":"Game of Thrones"
"q":"TV series"
"rank":29
"s":"Emilia Clarke, Peter Dinklage"
"v":[...]3 gadgets
"vt":302
"y":2011
"yr":"2011-2019"
}

I am making an attempt to particularly entry the imageUrl below i. Nonetheless, each time I attempt to entry it, it provides me the error “worth of kind ‘any’ has no subscripts” I believe my drawback has one thing to do with the Codable stuff, however I am unsure. Possibly I am overthinking it and I am not accessing the dictionary appropriately.

Right here is my code:

struct findMovieTitles: Codable {
let d: [[String:Any]]
let q: String
let v: Int

enum CodingKeys: String, CodingKey{
    case d, q, v
}

init(from decoder: Decoder) throws {
    let container = attempt decoder.container(keyedBy: CodingKeys.self)
    d = attempt container.decode([[String: Any]].self, forKey: .d)
    q = attempt container.decode(String.self, forKey: .q)
    v = attempt container.decode(Int.self, forKey: . v)
}

func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    attempt? container.encodeIfPresent(d, forKey: .d)
    attempt? container.encodeIfPresent(q, forKey: .q)
    attempt? container.encodeIfPresent(v, forKey: .v)
}

}

struct d: Codable{
    let i: [String: Any]
    let id: String
    let l: String
    let q: String
    let s: String
    let y: Int
    let yr: String

    enum CodingKeys: String, CodingKey{
        case i, id, l, q, s, y, yr
    }

    init(from decoder: Decoder) throws {
        let container = attempt decoder.container(keyedBy: CodingKeys.self)
        i = attempt container.decode([String:Any].self, forKey: .i)
        id = attempt container.decode(String.self, forKey: .id)
        l = attempt container.decode(String.self, forKey: .id)
        q = attempt container.decode(String.self, forKey: .q)
        s = attempt container.decode(String.self, forKey: .s)
        y = attempt container.decode(Int.self, forKey: .y)
        yr = attempt container.decode(String.self, forKey: .yr)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        attempt? container.encodeIfPresent(i, forKey: .i)
        attempt? container.encodeIfPresent(id, forKey: .id)
        attempt? container.encodeIfPresent(l, forKey: .l)
        attempt? container.encodeIfPresent(q, forKey: .q)
        attempt? container.encodeIfPresent(s, forKey: .s)
        attempt? container.encodeIfPresent(y, forKey: .y)
        attempt? container.encodeIfPresent(yr, forKey: .yr)
    }

}

struct i: Codable{
    let top: Int
    let imageUrl: String
    let width: Int
    
    enum CodingKeys: String, CodingKey{
        case top, imageUrl, width
    }
    
    init(from decoder: Decoder) throws {
        let container = attempt decoder.container(keyedBy: CodingKeys.self)
        top = attempt container.decode(Int.self, forKey: .top)
        imageUrl = attempt container.decode(String.self, forKey: .imageUrl)
        width = attempt container.decode(Int.self, forKey: .width)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        attempt? container.encodeIfPresent(top, forKey: .top)
        attempt? container.encodeIfPresent(imageUrl, forKey: .imageUrl)
        attempt? container.encodeIfPresent(width, forKey: .width)
    }
}

……

do {
            let jsonResult = attempt JSONDecoder().decode(findMovieTitles.self, from: information!)
            let dict = jsonResult.d[0]["i"]!
            print(dict["imageUrl"]) //this line provides me the error
          
            
        }
        catch{
            print(error)
        }

Any assist is far appreciated!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments