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
Robert Cathlina
1,747 PointsPlaylist Browser with Swift
I have completed the 'Build a Playlist Browser with Swift' course and my app functioned up until the last step or so. The problem started when I created all the playlistImageView variables and tried to append them to my playlistArray.
Here is my Playlist code: import 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)
artists += playlistDictionary["artists"] as! [String]
let largeIconName = playlistDictionary["largeIcon"] as! String
largeIcon = UIImage(named: largeIconName)!
let iconName = playlistDictionary["icon"] as! String
icon = UIImage(named: iconName)!
let colorsDictionary = playlistDictionary["backgroundColor"] as! [String: CGFloat]
// backgroundColor = rgbColorFromDictionary(colorsDictionary) // print(colorsDictionary) backgroundColor = UIColor(red: 255/255.0, green: 102/255.0, blue: 51/255.0, alpha: 1.0)
}
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)
}
Here is my Master View Controller code:
import UIKit
class PlaylistMasterViewController: UIViewController {
var playlistArray: [UIImageView!] = []
@IBOutlet weak var playlistImageView0: UIImageView!
@IBOutlet weak var playlistImageView1: UIImageView!
@IBOutlet weak var playlistImageView2: UIImageView!
@IBOutlet weak var playlistImageView3: UIImageView!
@IBOutlet weak var playlistImageView4: UIImageView!
@IBOutlet weak var playlistImageView5: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
playlistArray += [playlistImageView0, playlistImageView1, playlistImageView2, playlistImageView3, playlistImageView4, playlistImageView5]
for theIndex in 0..<playlistArray.count {
let playlist = Playlist(index: theIndex)
let playlistImageView = playlistArray[theIndex]
playlistImageView.image = playlist.icon
playlistImageView.backgroundColor = playlist.backgroundColor
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showPlaylistDetailSegue" {
let playlistImageView = sender!.view as! UIImageView
var index = playlistArray.indexOf(playlistImageView)
let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController
}
}
} @IBAction func showPlaylistDetail(sender: AnyObject) { performSegueWithIdentifier("showPlaylistDetailSegue", sender: sender)
}
I get the following error:
'Cannot convert value if type ‘UIImageView’ to expected argument type @noescape (UIUmageView!) throws -> Bool' and it appears in my Playlist file when I try and declare var index = playlistArray.indexOf(playlistImageView).
Please let me know your thoughts. Thanks!
1 Answer
Michael Reining
10,101 PointsHi Robert,
The indexOf method expects a closure. That closure takes an element of the array's type and returns a bool.
Here is an example:
let numbers: [Int] = [ 1, 2, 3, 4, 5, 6]
let index = numbers.indexOf {
$0 == 3
}
So I think if you update your code and use a closure it might work
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showPlaylistDetailSegue" {
let playlistImageView = sender!.view as! UIImageView
var index = playlistArray.indexOf {
$0 == playlistImageView
}
Hope that helps,
Mike
PS: Thanks to the awesome resources on Team Treehouse, I just launched my first app. :-)