DeyaEldeen
DeyaEldeen

Reputation: 11817

Why accessing this json sub elements by index raises this error?

this is the part of the json

        "feature_id" = (
            3047,
            3084,
            3095,
            3100,
            3121,
            3124,
            3182,
            3272,
            3273,
            3274
        );

this is how I access json, I use Alamofire.

var features_id = self.jsonD["results"]!["place_basic_info"]!!["feature_id"]!!

now the problem is in here

features_id[0] as? String

the error raised is :

ambiguous use of 'subscript'

the weird thing is this shows up when I try to build for the device, but not when running, how to solve this problem ? and why it shows up?

Upvotes: 1

Views: 48

Answers (1)

Eric Aya
Eric Aya

Reputation: 70098

ambiguous use of 'subscript'

The issue is that the compiler doesn't know that features_id is an array, so it is unable to subscript it by index.

You have to give the object's type to the compiler, for example with optional binding and casting:

if let featID = features_id as? [Int] {
    // here featID is features_id unwrapped as an array of Ints
}

Upvotes: 1

Related Questions