-1

I am getting this error at the line if let arrJson = allContacts["contacts"]

in the below code:

if let arrJson = allContacts["contacts"]     {
            for index in 0...arrJson.count - 1
            {
                let aObject = arrJson[index] as! [String : AnyObject]

                names.append(aObject["name"] as! String)
                contacts.append(aObject["email"] as! String)

                }
            }

Please help me what to do? I have tried this one but not solved this error Type 'Any' Has no Subscript Members in xcode 8 Swift 3

Community
  • 1
  • 1
user7356913
  • 15
  • 3
  • 6

2 Answers2

1

Try this :

let tempDic = allContacts as! Dictionary<String,Any>
        if let arrJson = tempDic["contacts"]      {

            let arrJson = arrJson as! Array<Dictionary<String,Any>>
            for index in 0...(arrJson).count - 1
            {
                let aObject = arrJson[index] as [String : AnyObject]

                names.append(aObject["name"] as! String)
                contacts.append(aObject["email"] as! String)

            }
        }
Jagdeep Singh
  • 2,556
  • 3
  • 18
  • 28
1

You are getting an error b'coz you allContacts not declared as Array, Dictionary or Set (these collection has subscripts).

So what you all need to do is to cast allContacts in appropriate type.

eg.

`allContacts as! Dictionary<AnyHashable, Any>`
`allContacts as! Array<Any>`
Mahendra
  • 8,448
  • 3
  • 33
  • 56