Error trying to access members of Array of AnyObject within a Dictionary - Is there a way around unwrapping?

ios, swift

Solution

`Dictionary` always give you `Optional` value.

Your code is like this

let thisArray : Optional<AnyObject[]> = jDict[key]

You need to unwrap it to get non-optional value

let thisArray = jDict[key]! // thisArray is AnyObject[]

Problem

I have a dictionary set up as: ``` var jDict = Dictionary<String, AnyObject[]>() ``` Where the arrays are either a collection of custom buttons (JunkButton) or Labels (JunkLabels). I am having an issue when trying to access the members of the arrays contained in the Dictionary as follows: ``` let thisArray = jDict[key] var aButton = thisArray[0] //Gives error: 'AnyObject[]? does not have a member named 'subscript' ``` I can get around this by downcasting the whole array as follows: ``` if let aArray = thisArray as? JunkButton[]{ var aButton = aArray[0] } ``` This seems very cumbersome especially if I am sure I know what type the array is made up of beforehand. Is there a way to cast thisArray when it is created that would allow me to extract its elements without unwrapping them each time?

Original source