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 Refactoring Our Code Wrapping Up!

Piotr Reshetin
Piotr Reshetin
1,815 Points

New feature: a mutable ordered list of playlist artists

Just want to share my feature.

Currently each playlist contains list of artists of the same size. But in case of different number of artists the existing code will throw a runtime error on navigating of playlist.

Ok, here is my sollution.

At PlaylistDetailViewController.swift add:

var artists: [UILabel?] = []

Then inside viewDidLoad() function add:

artists = [
                playlistArtist0,
                playlistArtist1,
                playlistArtist2,
                playlistArtist3,
                playlistArtist4,
                playlistArtist5,
                playlistArtist6
            ]
let numberOfArtists = artists.count

for index in 0..<numberOfArtists {
    if index < playlist!.artists?.count {
        artists[index]!.text = "\(index + 1). \((playlist!.artists?[index])!)"
    } else {
        artists[index]!.text = ""
    }
}

That's it! To check everything is working fine, go to MusicLibrary.swift and at any playlist remove a few artists. And then run your app and tap to that playlist.

Hope this helps someone!

1 Answer

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Great job Piotr! Great to see somebody putting what they know into practice!