I've seen this solved in other questions - but I think that because this NSDictionary is accessed via subscript it's throwing some errors.
func pickRandomRecipe(arrayOfRecipes: NSArray) -> Dictionary<String,Any> {
let randomRecipeIndex = Int(arc4random_uniform(UInt32(arrayOfRecipes.count)))
//Could not cast value of type '__NSDictionaryI' (0x7fbfc4ce0208) to 'Swift.Dictionary<Swift.String, protocol<>>' (0x7fbfc4e44358)
let randomRecipe: Dictionary = arrayOfRecipes[randomRecipeIndex] as! Dictionary<String,Any>
return randomRecipe
}
In this case
NSDictionary
can only be casted to[String: NSObject]
. If you want it to be of type[String : Any]
you have to make a separate Dictionary:NSDictionary should be bridged to
[NSCopying: AnyObject]
or in your case[String: AnyObject]
, rather than usingAny
(since that's a Swift-only construct).But I would recommend not using NSDictionary at all. You could define your function as
Or perhaps even better: