user5921637
user5921637

Reputation: 11

type (string , anyobject) has no subscript member

I use dataTaskWithRequest and get json of array with two objects in it (these objects are key,value) and I want check one value of key in two objects.

this is my code :

let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
            print("Response: \(response)")

            var jsonArray: [String:AnyObject]!

            do {
                jsonArray = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as? [String:AnyObject]
            } catch {
                print(error)
            }

            for json in jsonArray {
                print("object json reciver :",json)

//type (string , anyobject) has no subscript member
                print("state :",json["state"])

            }
        })

Upvotes: 1

Views: 5081

Answers (2)

Marlon Ruiz
Marlon Ruiz

Reputation: 1842

You need to cast jsonArray to NSArray.

Upvotes: -3

Vatsal
Vatsal

Reputation: 18181

json is a variable typed (String, AnyObject). You cannot subscript tuples.

Replace the following:

print("state :",json["state"])

with:

print("\(json.0) : \(json.1)")

Upvotes: 6

Related Questions