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 trialChris Hunter
2,091 PointsI get compiler errors in the playlist.swift file stating: " 'NSDictionary' is not a subtype of 'NSString'?
I even tried adding the playlist.swift file in the package download and it doesn't work. Thanks.
8 Answers
Steve Hunter
57,712 PointsCan you post your code, please. I've got my code open so I can hunt down the difference.
Thanks.
Chris Hunter
2,091 Pointsimport Foundation import UIKit
struct Playlist {
var title: String?
var description: String?
var icon: UIImage?
var largeIcon: UIImage?
var artists: [String] = []
var backgroundColor: UIColor = UIColor.clearColor()
init(index: Int) {
let musicLibrary = MusicLibrary().library
let playlistDictionary = musicLibrary[index]
title = playlistDictionary["title"] as String!
description = playlistDictionary["description"] as String!
let iconName = playlistDictionary["icon"] as String!
icon = UIImage(named: iconName)
let largeIconName = playlistDictionary["largeIcon"] as String!
largeIcon = UIImage(named: largeIconName)
artists += playlistDictionary["artists"] as [String]
let colorsDictionary = playlistDictionary["backgroundColor"] as [String:CGFloat]
backgroundColor = rgbColorFromDictionary(colorsDictionary)
}
func rgbColorFromDictionary(colorDictionary: [String:CGFloat]) -> UIColor {
let red = colorDictionary["red"]!
let green = colorDictionary["green"]!
let blue = colorDictionary["blue"]!
let alpha = colorDictionary["alpha"]!
return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
}
}
Chris Hunter
2,091 PointsHowever, as I said, I copied and pasted the code from the assets posted for that file because it was giving me errors. Could it be elsewhere in my project that is giving me the problems? Thanks for your help!
Steve Hunter
57,712 PointsJust reformatting your post ... same code ...
struct Playlist {
var title: String?
var description: String?
var icon: UIImage?
var largeIcon: UIImage?
var artists: [String] = []
var backgroundColor: UIColor = UIColor.clearColor()
init(index: Int) {
let musicLibrary = MusicLibrary().library
let playlistDictionary = musicLibrary[index]
title = playlistDictionary["title"] as String!
description = playlistDictionary["description"] as String!
let iconName = playlistDictionary["icon"] as String!
icon = UIImage(named: iconName)
let largeIconName = playlistDictionary["largeIcon"] as String!
largeIcon = UIImage(named: largeIconName)
artists += playlistDictionary["artists"] as [String]
let colorsDictionary = playlistDictionary["backgroundColor"] as [String:CGFloat]
backgroundColor = rgbColorFromDictionary(colorsDictionary)
}
func rgbColorFromDictionary(colorDictionary: [String:CGFloat]) -> UIColor {
let red = colorDictionary["red"]!
let green = colorDictionary["green"]!
let blue = colorDictionary["blue"]!
let alpha = colorDictionary["alpha"]!
return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha)
}
}
Steve Hunter
57,712 PointsWhich line is causing the problem? Where's the compiler error?
Chris Hunter
2,091 PointsI get that error on lines:
title = playlistDictionary["title"] as String!
let iconName = playlistDictionary["icon"] as String!
let largeIconName = playlistDictionary["largeIcon"] as String!
artists += playlistDictionary["artists"] as [String]
let colorsDictionary = playlistDictionary["backgroundColor"] as [String:CGFloat]
Thanks again!
Steve Hunter
57,712 PointsWeird!
This might be related to how the Playlist struct is being initialised since we are in the init
method.
That takes an Int
to set the part of the library it is looking at. It is called from viewDidLoad()
within the MasterViewController
. Mine says:
let playlist = Playlist(index: index)
inside a For loop.
I'm not sure this is relevant, to be honest. I'll give it some thought and sleep on it (22:43 here). Something may come to me. For some reason, though, your Dictionary
is colliding with an NSString
object. I can't see where it is doing that, though.
Chris Hunter
2,091 PointsI figured it out, I had an extra set of square brackets in my dictionary in MusicLibrary. I don't know how I missed that before. Thank you very much for your help!