Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift,字典解析错误_Swift_Xcode_Dictionary_Nsdictionary - Fatal编程技术网

Swift,字典解析错误

Swift,字典解析错误,swift,xcode,dictionary,nsdictionary,Swift,Xcode,Dictionary,Nsdictionary,我正在使用API获取天气状况,检索到的dict是 dict = { base = stations; clouds = { all = 92; }; cod = 200; coord = { lat = "31.23"; lon = "121.47"; }; dt = 1476853699; id = 1796231; main = {

我正在使用API获取天气状况,检索到的dict是

dict = {
    base = stations;
    clouds =     {
        all = 92;
    };
    cod = 200;
    coord =     {
        lat = "31.23";
        lon = "121.47";
    };
    dt = 1476853699;
    id = 1796231;
    main =     {
        "grnd_level" = "1028.63";
        humidity = 93;
        pressure = "1028.63";
        "sea_level" = "1029.5";
        temp = "73.38";
        "temp_max" = "73.38";
        "temp_min" = "73.38";
    };
    name = "Shanghai Shi";
    rain =     {
        3h = "0.665";
    };
    sys =     {
        country = CN;
        message = "0.0125";
        sunrise = 1476827992;
        sunset = 1476868662;
    };
    weather =     (
        {
            description = "light rain";
            icon = 10d;
            id = 500;
            main = Rain;
        }
    );
    wind =     {
        deg = "84.50239999999999";
        speed = "5.97";
    };
}
如果我想要湿度的值,我只需要使用

让humidityValue=dict[“main”][“湿度”]
就可以了

但问题是我还想在天气中获得描述的值

当我使用
时,让dscptValue=dict[“weather”][“description”]

它检索到零

怎么样?我注意到在weather周围有两个括号。我不确定没有括号的语句是否相同

    weather =     (
            {
        description = "light rain";
        icon = 10d;
        id = 500;
        main = Rain;
    }
);

如何获取说明的值?

天气是一系列字典

dict["weather"][0]["description"] 

天气是一系列的字典

dict["weather"][0]["description"] 

可能会给您预期的结果。

weather
键包含
字典的
数组
,而不是直接
字典
,因此您需要访问它的第一个对象

if let weather = dict["weather"] as? [[String: AnyObject]], let weatherDict = weather.first {
    let dscptValue = weatherDict["description"]
}

注意:我使用了带有
if let
的可选包装,以防止强制包装导致崩溃。

天气
键包含
字典
数组
,而不是直接
字典
,因此您需要访问它的第一个对象

if let weather = dict["weather"] as? [[String: AnyObject]], let weatherDict = weather.first {
    let dscptValue = weatherDict["description"]
}
注意:我使用了可选包装和
if let
来防止强制包装崩溃