-1

I have written code in swift 2.3 while converting to swift 3 i am getting error like "type Any has no subscript members"

let userDetail:Any = (GJCommon.sharedInstance.getUserInfo() as? NSDictionary)!

    print(userDetail)

    let userImage = (userDetail["Vehicle"]["bike_photo"] as? String)!

In this line i am getting error "let userImage = (userDetail["Vehicle"]["bike_photo"] as? String)!" Please help to do this.

Vijay
  • 1
  • 1
  • 3
    I can see at least 6 questions with the same title in the "Related" section on the right. Are you sure that none of them helps to solve your problem? – Martin R Sep 19 '16 at 07:47
  • Yes here my error is getting values from dictionary. no one cleared my doubt. – Vijay Sep 19 '16 at 07:48
  • http://stackoverflow.com/questions/39466624/ambiguous-use-of-subscript-after-converting-to-swift-2-3/39466734#39466734 – Bhavin Bhadani Sep 19 '16 at 07:48
  • what is the output of `GJCommon.sharedInstance.getUserInfo()`? – Bista Sep 19 '16 at 08:02
  • 2
    Possible duplicate of [type any? has no subscript members](http://stackoverflow.com/questions/38956785/type-any-has-no-subscript-members) – Zeeshan Sep 19 '16 at 08:25

2 Answers2

1

Try this:

if let userDetail = GJCommon.sharedInstance.getUserInfo() as? [String:AnyObject] {

    print(userDetail)

    let userImage = (userDetail["Vehicle"]["bike_photo"] as? String)!
}
Bista
  • 7,869
  • 3
  • 27
  • 55
1

You are trying to access userDetail as an NSDictionary but it is of type Any.

As #Mr.UB said (beat me to it), change the first line to:

let userDetail = (GJCommon.sharedInstance.getUserInfo() as? NSDictionary)!

And it should then compile, although other parts of your code may then object. More context would be useful.

However your use of ! to force unwrap this and a later result is a bad idea. Use conditional unwrapping to catch errors more gracefully and avoid an App that crashes regularly.

Ali Beadle
  • 4,486
  • 3
  • 30
  • 55