0

While trying to get a value from a Dictionary, Xcode is showing me the following error:

Could not find an overload for 'subscript' that accepts the supplied arguments

class func fromDictionary(enterpriseDictionary:Dictionary<String, AnyObject>) -> Enterprise
{
    var enterprise:Enterprise = Enterprise()

    enterprise.id = enterpriseDictionary["id"] as Int  (ERROR OCCURS HERE)
    enterprise.name = enterpriseDictionary["name"] as? String ( AND HERE )


    return enterprise
}
jscs
  • 63,694
  • 13
  • 151
  • 195
adheus
  • 3,985
  • 2
  • 20
  • 33

1 Answers1

0

I managed to resolve this question by replacing the "AnyObject" by "Any" as seen below:

class func fromDictionary(enterpriseDictionary:Dictionary<String, Any>) -> Enterprise
{
    var enterprise:Enterprise = Enterprise()

    enterprise.id = enterpriseDictionary["id"] as Int
    enterprise.name = enterpriseDictionary["name"] as? String


    return enterprise
}

As @Andrey Tarantsov said:

This occurs because AnyObject is for any class instance (you can also think of it as objects in Objective-C sense); String and Int are not classes.

adheus
  • 3,985
  • 2
  • 20
  • 33
  • 2
    Because AnyObject is for any _class instance_ (you can also think of it as objects in Objective-C sense); String and Int are not classes. – Andrey Tarantsov Jun 04 '14 at 07:45