在将其解析为JSON文件Swift之后声明类型

时间:2016-04-09 14:51:20

标签: ios json xcode

现在我有一个JSON输出,我从Pearson的API获得:

Optional(<__NSCFArray 0x7f9b7601a910>(
{
    datasets =     (
        laad3,
        dictionary
    );
    headword = theoretically;
    id = cs1tNTvYvg;
    "part_of_speech" = adverb;
    senses =     (
                {
            definition = "used to say that something could happen, but it is extremely unlikely";
            examples =             (
                                {
                    text = "It\U2019s theoretically possible for everyone in the class to get 100%.";
                }
            );
        }
    );
    url = "/v2/dictionaries/entries/cs1tNTvYvg";
},
{
    datasets =     (
        laad3,
        dictionary
    );
    headword = theoretical;
    id = cs1tNTpDbZ;
    "part_of_speech" = adjective;
    senses =     (
                {
            definition = "relating to or based on ideas, especially scientific ideas, rather than practical work or experience";
            examples =             (
                                {
                    text = "theoretical research";
                }
            );
        }
    );
    url = "/v2/dictionaries/entries/cs1tNTpDbZ";
})

并假设上面的输出存储到:

let results = parsedResult!["results"]

XCode表示对下标的使用含糊不清,在谷歌搜索之后,似乎我必须继续使用结果类型。我需要将results的类型转换为例如[[String:String]]x=(ed1.getText().toString()); y=(ed2.getText().toString()); z=(ed3.getText().toString()); xx=x+""+y+""+z;

旁注:有人知道为什么Xcode会抱怨在使用iOS 9.3的iPhone6上选择运行目标时使用下标,但在模拟器中运行时却没有(在带有iOS9.3的iPhone6上) ?

1 个答案:

答案 0 :(得分:1)

我不明白这个问题的深层原因。但是自从XCode 7.3(或者更确切地说是Swift 2.2)以来,编译器现在抱怨在使用NSDictionary模糊地使用下标

为避免此错误,请将NSDictionary个实例投放到[String: AnyObject]NSArray个实例,以[AnyObject]