Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to cast this data in swift.

In my variable I have this:

["data": (
        {
        apparentTemperatureMax = "65.31";
        apparentTemperatureMaxTime = 1444143600;
        apparentTemperatureMin = "57.36";
        apparentTemperatureMinTime = 1444168800;
        cloudCover = "0.64";
        dewPoint = "56.94";
        humidity = "0.86";
        icon = rain;
        moonPhase = "0.8";
        ozone = "303.42";
        precipIntensity = "0.0056";
        precipIntensityMax = "0.0239";
        precipIntensityMaxTime = 1444086000;
        precipProbability = "0.54";
        precipType = rain;
        pressure = "997.29";
        summary = "Drizzle starting in the afternoon, continuing until evening.";
        sunriseTime = 1444111790;
        sunsetTime = 1444152604;
        temperatureMax = "65.31";
        temperatureMaxTime = 1444143600;
        temperatureMin = "57.36";
        temperatureMinTime = 1444168800;
        time = 1444086000;
        visibility = "6.48";
        windBearing = 166;
        windSpeed = "6.89";
    },
        {
        apparentTemperatureMax = "59.97";
        apparentTemperatureMaxTime = 1444230000;
        apparentTemperatureMin = "50.23";
        apparentTemperatureMinTime = 1444255200;
        cloudCover = "0.72";
        dewPoint = "49.54";
        humidity = "0.78";
        icon = rain;
        moonPhase = "0.83";
        ozone = "313.62";
        precipIntensity = "0.0028";
        precipIntensityMax = "0.0094";
        precipIntensityMaxTime = 1444186800;
        precipProbability = "0.44";
        precipType = rain;
        pressure = "1009.55";
        summary = "Light rain in the morning.";
        sunriseTime = 1444198289;
        sunsetTime = 1444238869;
        temperatureMax = "59.97";
        temperatureMaxTime = 1444230000;
        temperatureMin = "50.23";
        temperatureMinTime = 1444255200;
        time = 1444172400;
        visibility = "8.699999999999999";
        windBearing = 283;
        windSpeed = "10.08";
    },
        {
        apparentTemperatureMax = "61.91";
        apparentTemperatureMaxTime = 1444316400;
        apparentTemperatureMin = "42.83";
        apparentTemperatureMinTime = 1444280400;
        cloudCover = "0.21";
        dewPoint = "42.28";
        humidity = "0.68";
        icon = "partly-cloudy-day";
        moonPhase = "0.86";
        ozone = "314.92";
        precipIntensity = 0;
        precipIntensityMax = 0;
        precipProbability = 0;
        pressure = "1021.17";
        summary = "Partly cloudy starting in the afternoon.";
        sunriseTime = 1444284788;
        sunsetTime = 1444325134;
        temperatureMax = "61.91";
        temperatureMaxTime = 1444316400;
        temperatureMin = "45.18";
        temperatureMinTime = 1444284000;
        time = 1444258800;
        visibility = "9.380000000000001";
        windBearing = 294;
        windSpeed = "3.97";
    },
        {
        apparentTemperatureMax = "62.37";
        apparentTemperatureMaxTime = 1444402800;
        apparentTemperatureMin = "44.65";
        apparentTemperatureMinTime = 1444370400;
        cloudCover = "0.08";
        dewPoint = "43.52";
        humidity = "0.71";
        icon = "partly-cloudy-night";
        moonPhase = "0.89";
        ozone = "319.29";
        precipIntensity = 0;
        precipIntensityMax = 0;
        precipProbability = 0;
        pressure = "1022.8";
        summary = "Partly cloudy overnight.";
        sunriseTime = 1444371287;
        sunsetTime = 1444411400;
        temperatureMax = "62.37";
        temperatureMaxTime = 1444402800;
        temperatureMin = "44.65";
        temperatureMinTime = 1444370400;
        time = 1444345200;
        visibility = 10;
        windBearing = 84;
        windSpeed = "3.35";
    },
        {
        apparentTemperatureMax = "59.45";
        apparentTemperatureMaxTime = 1444485600;
        apparentTemperatureMin = "42.06";
        apparentTemperatureMinTime = 1444514400;
        cloudCover = "0.57";
        dewPoint = "46.79";
        humidity = "0.79";
        icon = "partly-cloudy-day";
        moonPhase = "0.92";
        ozone = "303.67";
        precipIntensity = "0.0004";
        precipIntensityMax = "0.0014";
        precipIntensityMaxTime = 1444489200;
        precipProbability = "0.03";
        precipType = rain;
        pressure = "1019.42";
        summary = "Partly cloudy throughout the day.";
        sunriseTime = 1444457787;
        sunsetTime = 1444497667;
        temperatureMax = "59.45";
        temperatureMaxTime = 1444485600;
        temperatureMin = "46.46";
        temperatureMinTime = 1444514400;
        time = 1444431600;
        visibility = 10;
        windBearing = 96;
        windSpeed = "8.5";
    },
        {
        apparentTemperatureMax = "58.91";
        apparentTemperatureMaxTime = 1444568400;
        apparentTemperatureMin = "39.7";
        apparentTemperatureMinTime = 1444525200;
        cloudCover = "0.01";
        dewPoint = "45.03";
        humidity = "0.83";
        icon = "clear-day";
        moonPhase = "0.95";
        ozone = "301.09";
        precipIntensity = 0;
        precipIntensityMax = 0;
        precipProbability = 0;
        pressure = "1011.89";
        summary = "Clear throughout the day.";
        sunriseTime = 1444544288;
        sunsetTime = 1444583934;
        temperatureMax = "58.91";
        temperatureMaxTime = 1444568400;
        temperatureMin = "44.89";
        temperatureMinTime = 1444525200;
        time = 1444518000;
        visibility = "9.32";
        windBearing = 85;
        windSpeed = "13.61";
    },
        {
        apparentTemperatureMax = "56.44";
        apparentTemperatureMaxTime = 1444658400;
        apparentTemperatureMin = "37.81";
        apparentTemperatureMinTime = 1444622400;
        cloudCover = "0.02";
        dewPoint = "42.99";
        humidity = "0.8100000000000001";
        icon = "partly-cloudy-night";
        moonPhase = "0.98";
        ozone = "302.68";
        precipIntensity = 0;
        precipIntensityMax = 0;
        precipProbability = 0;
        pressure = "1014.16";
        summary = "Partly cloudy overnight.";
        sunriseTime = 1444630788;
        sunsetTime = 1444670202;
        temperatureMax = "56.44";
        temperatureMaxTime = 1444658400;
        temperatureMin = "44.05";
        temperatureMinTime = 1444622400;
        time = 1444604400;
        windBearing = 51;
        windSpeed = "13.22";
    },
        {
        apparentTemperatureMax = "55.08";
        apparentTemperatureMaxTime = 1444741200;
        apparentTemperatureMin = "38.7";
        apparentTemperatureMinTime = 1444712400;
        cloudCover = "0.6899999999999999";
        dewPoint = "41.15";
        humidity = "0.76";
        icon = "partly-cloudy-day";
        moonPhase = "0.01";
        ozone = "306.43";
        precipIntensity = 0;
        precipIntensityMax = 0;
        precipProbability = 0;
        pressure = "1019.13";
        summary = "Mostly cloudy throughout the day.";
        sunriseTime = 1444717289;
        sunsetTime = 1444756471;
        temperatureMax = "55.08";
        temperatureMaxTime = 1444741200;
        temperatureMin = "44.5";
        temperatureMinTime = 1444712400;
        time = 1444690800;
        windBearing = 33;
        windSpeed = "11.8";
    }
), “summary”: Light rain today and tomorrow, with temperatures falling to 55°F on Tuesday., "icon": rain]

And I want to know how to cast this data so all attributes are reachable. I suppose that it’s JSON format but currently is casted as [String:Anyobject].

1 Answer

Hey César,

The reason you're getting a dictionary of those types is because it currently does not contain homogenous data (data of the same type). To fix this, you'll have to check whether or not the specific types for the key, value pairs are there. You can do that by type casting.

if let dict = someData as? [String: Int] {
   // do something here
}

This checks whether someData is of [String: Int] type. It'll bind the value if it is, otherwise it'll return nil.

Hope I've helped. Happy Coding!