Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Playlist Browser with Swift Building the Music Library and Playlist Models Using a Playlist Instance

Carson Carbery
Carson Carbery
9,876 Points

Cannot subscript a value of type '[NSDictionary]' with an index of type 'String'

Hi all,

I get the error above when trying to define the init method in this lesson. The error occurs each time I try and assign a value from the PlayListDictionary. For example the two lines below give the error for each line:

title = playListDictionary["title"] as String! description = playListDictionary["description"] as String!

I have checked the type and see that indeed playListDictionary is indeed an NSDictionary (but isn't that an objective C dictionary, so what is it doing here). I've double checked the code and it is exactly the same as that used by the teacher Pasan in this lesson.

Please help as I can not continue with the lesson without fixing this.

Many thanks

1 Answer

Tobias Helmrich
Tobias Helmrich
31,603 Points

Hey Carson,

this course uses an older version of Swift but since Swift 1.2 you have to use the as! operator when you want to force downcast to a type instead of writing an exclamation mark after the type you want to downcast to.

So if you change this

title = playListDictionary["title"] as String! 
description = playListDictionary["description"] as String!

to this

title = playListDictionary["title"] as! String
description = playListDictionary["description"] as! String

it should work. If it doesn't or if you have further questions please let me know. I hope that helps! :)