0

I am trying to change my code to swift 2.1 to swift 3

but I am getting following error :

Type Any? has no subscript members

Earlier I was using

let activityCount = self.activityCoutArray.value(forKey: "ActivitytodoCount")[0].value(forKey: "Count") as! Int

and I have changed to Swift 3

let activityCount = self.activityCoutArray["ActivitytodoCount"][0]["Count"] as? [String:Any]

but still it is giving same error ,

Please help me where I am going wrong..

Raghav Chopra
  • 527
  • 1
  • 10
  • 26
  • In Swift 3 the compiler must know the actual type of all subscripted objects for type safety reasons. That kind of chaining is not supported (anymore). – vadian Jan 24 '17 at 11:30
  • Possible duplicate of [Type 'Any' Has no Subscript Members in xcode 8 Swift 3](http://stackoverflow.com/questions/39516199/type-any-has-no-subscript-members-in-xcode-8-swift-3) – user28434'mstep Jan 24 '17 at 11:49
  • @user28434 My query here is different for swift not fir jquery – Raghav Chopra Jan 25 '17 at 05:49
  • @RaghavChopra, doesn't matter which query you have, root problem is same, and is fixable in same way. – user28434'mstep Jan 25 '17 at 07:48

1 Answers1

1

It should be like this. First get array of dictionary([[String:Any]]) and then access the first element from array after that get the value from Dictionary that you want.

if let activityArray = self.activityCoutArray["ActivitytodoCount"] as? [[String:Any]],
   let firstDic = activityArray.first, let count = firstDic["count"] as? Int {
      print(count)
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183