Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

2 Answers

Tobias Helmrich
Tobias Helmrich
31,601 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! :)

Carson Carbery
Carson Carbery
9,876 Points

Hi Tobias,

I've made the change you suggested, so....

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

However I am getting exactly the same error :-/

Tobias Helmrich
Tobias Helmrich
31,601 Points

Could you please post the whole code for your Playlist.swift file? :)

Carson Carbery
Carson Carbery
9,876 Points

Sure here it is, thanks so much for checking through it:

// // Playlist.swift // Algorhythm

import Foundation import UIKit

struct Playlist { var title: String? var description: String? var icon: UIImage? var largeIcon: UIImage? var artist: [String] = [] var backGroundColor: UIColor = UIColor.clearColor()

init(index: Int){
    let musicLibrary = MusicLibrary().library
    let playListDictionary = musicLibrary

    title = playListDictionary["title"] as! String

    description = playListDictionary["description"] as! String

}

}

Tobias Helmrich
Tobias Helmrich
31,601 Points

Sure, no problem! :) Make sure to specify the index of musicLibrary with the index you pass to the init method when you assign it to playListDictionary .

Like so:

let playListDictionary = musicLibrary[index]

I hope that fixes the problem, if not let me know!

Carson Carbery
Carson Carbery
9,876 Points

Hi Tobias, I have just noticed that I missed adding in the index value on the musicLibrary, when I add that your answer above with the as! now seems to be working. Thanks very much for the help, I can progress now.

Tobias Helmrich
Tobias Helmrich
31,601 Points

No problem, I'm glad you got it working! :)