Having problems accessing data out of dictionary.

I have successfully received some JSON. Ran it through JSONSerialization and created a dictionary from. I can access any key: value pair but I can't seem to access a key with what I want to call nested properties/objects. I am new to Swift so I am unsure of terminology. Anyways here is an example of the key I want to access.

"videos": {

    mp4 =     {

        1080 = "https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_1080.mp4";

        240 = "https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_240.mp4";

        360 = "https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_360.mp4";

        480 = "https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_480.mp4";

    };

Here is the chunk of swift I would have thought found what I want:

if let link = movieDict["videos.mp4"]?["480"] as? String? {

            print(link)

            self.videoLink = link

        }

Here is the swift file containing the structs:

class Movie{

    

    struct Welcome: Codable {

        let error: Bool

        let message: String

        let response: Response

    }

    

    struct Response: Codable {

        let totalRows: Int

        let rows: [Row]

    }

    

    struct Row: Codable {

        let title, cleanTitle: String

        let videos: Videos

        let poster, thumbnail: String

    }

    

    struct videos: Codable {

        let vides: [String: String]

    }

    

    enum TypeEnum: String, Codable {

        case video = "video"

    }

    

    struct Videos: Codable {

        let mp4: [String: String]

    }

    

    var title : String!

    var posterPath : String!

    var videoLink : String!

Please help! I have been banging my head against the wall!

Thanks!

Greg

if let link = movieDict["videos.mp4"]?["480"]...

With that line of code you're trying to access the element in the dictionary called videos.mp4. There is no element with that name.

You need to use something like if let link = movieDict["videos"]["mp4"]["480"]...

Thank you! I appreciate the response!

I tried and I am getting the following:

Value of optional type 'AnyObject?' must be unwrapped to refer to member 'subscript' of wrapped base type 'AnyObject' Chain the optional using '?' to access member 'subscript' only for non-'nil' base values Force-unwrap using '!' to abort execution if the optional value contains 'nil' Value of type 'Any??' has no subscripts

I add '?' in between videos and mp4 like the error above suggestions and I get the following:

Value of type 'Any??' has no subscripts

Where do you add the ?

please show code.

So here my current code.

if let link = movieDict["videos"]?["mp4"]?["480"]? as? String? {

            print(link)

            self.videoLink = link

        }

It is getting: Type of expression is ambiguous without more context

Cut down what you're trying to do so it's easier to figure out what's going wrong.

Start with accessing the videos element. If it's there and can be accessed, try to access the mp4 element, and so on.

At some point you'll either find the problem, or you won't experience the problem because your code is working properly.

So, you have movieDict which is a Dictionary<String, Any>, I guess? (I've added line numbers so I can refer to them in a bit)

1: videos: {
2:   mp4 =  {
3:    1080 = "https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_1080.mp4";
4:    240 = "https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_240.mp4";
5:    360 = "https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_360.mp4";
6:    480 = "https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_480.mp4";
7:  };

Are you sure the mp4 line is mp4 = { and not mp4: {?

Anyway:

let movieDict: Dictionary<String, Any> = <whatever function gives you the dictionary>
let videosDict: Dictionary<String, Any> = movieDict["videos"] as! Dictionary<String, Any> // This gets the 'videos' item as a Dictionary, line 1. ! means crash if it's nil
let mp4Dict: Dictionary<String, String> = videosDict["mp4"] as! Dictionary<String, String> // This gives you the Dictionary of 'mp4', lines 2-7. Again, ! means crash if it's nil
print(videosDict)
print(mp4Dict)
print(mp4Dict["1080"]) // Should print https://videos.kaiserclix.com/AVideo/videos/_YPTuniqid_6061fbc1c89a53.50466226_1080.mp4

This is all pseudo-code and I haven't actually written this in Xcode, but it should be pretty easy to work with.

Having problems accessing data out of dictionary.
 
 
Q