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 Struct Initialization

title = playlistDictionary["title"] as String! I am getting '(String, NSObject) is not convertible to String' error?

i have tried changing the code to

title = playlistDictionary["title"]! as? String

it was something i found on the internet but this also doesn't seem to work. please help!

4 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Aman,

Try the following instead as you're attempting to unwrap the dictionary value then down cast it which won't work in this case.

title = playlistDictionary["title"] as String

Hope that helps.

Alx Ki
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alx Ki
Python Web Development Techdegree Graduate 14,822 Points

Hi, Chris,

Maybe you have another solution? I even installed old xcode 6.0.1, but still have that error. (String, NSObject) is not convertible to String.

Hey Chris,

The original code I tried was:

title = playlistDictionary["title"] as String!

I also tried your code above and got the same error: '(String, NSObject)' is not convertible to 'String'

Would you have any other idea what could be happening?

HO WANG CHENG
HO WANG CHENG
8,694 Points

Hi Aman

try

title = playlistDictionary["title"] as NSString

I know this was asked a while ago... Anyways, I don't know if there is a Swift version discrepancy, but I don't understand why the speaker is unwrapping the result, when he is assigning it to an optional anyway - title:String? still needs to be unwrapped when accessed later. The following should work...

title = playlistDictionary["title"] as? String

Nb. Note the ? trailing 'as' which is the only difference to Chris' suggestion :)