Pangu
Pangu

Reputation: 3819

How to resolve "ambiguous use of subscript" error?

I'm having an issue in Swift where my code was working perfectly fine. Upon importing the AVFoundation, I get the ambiguous use of subscript error for the item

I found the reason why from here: Ambiguous use of ‘subscript’ error when importing AVFoundation

However, I can't seem to resolve it based on the answer provided there.

Here's my code:

self.resultsVideoDurations_DICT = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! Dictionary<NSObject, AnyObject>

let item = self.resultsVideoDurations_DICT["items"] as AnyObject!

let key  =  ( ( ( item[0] as! Dictionary<NSObject, AnyObject>)["snippet"] as! Dictionary<NSObject, AnyObject> )["publishedAt"] as? String)!

let value = ( ( item[0] as! Dictionary<NSObject, AnyObject>)["contentDetails"] as! Dictionary<NSObject, AnyObject> )["duration"] as? String

I have tried to do the following (items[0])! but that gives me the warning:

Cast from 'AUAudioUnitBus' to unrelated type 'Dictionary' always fails

How can I resolve this? Thanks

Upvotes: 0

Views: 128

Answers (1)

Alex Curylo
Alex Curylo

Reputation: 4770

Simply cast item to the type you proceed to force it to:

let item = self.resultsVideoDurations_DICT["items"] as! [Dictionary<NSObject, AnyObject>]

let key  =  ( ( ( item[0])["snippet"] as! Dictionary<NSObject, AnyObject> )["publishedAt"] as? String)!

let value = ( ( item[0])["contentDetails"] as! Dictionary<NSObject, AnyObject> )["duration"] as? String

That appears to clear it out in a playground.

Upvotes: 0

Related Questions