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

Martin Foldyna
Martin Foldyna
10,359 Points

Swift index error

I'm getting error like this 'fatal error: Array index out of range'.

Here is my code:

init (index: Int) {
        let brandsLibrary = BrandsLibrary().library
        let playlistDictionary = brandsLibrary[index]
        brand = playlistDictionary ["brand"] as! String!
        clas = playlistDictionary["class"] as! String!
        headquaters = playlistDictionary["headquaters"] as! String!
        models += playlistDictionary["models"] as! [String]        
    }

Can somebody please help me how to get rid of this error?

Martin Foldyna
Martin Foldyna
10,359 Points

Brendan Whiting unfortunately that's not it, but thanks for your help :)

Martin Foldyna
Martin Foldyna
10,359 Points

Brendan Whiting unfortunately that's not it, but thanks for your help :)

1 Answer

"index out of range" means you are trying to access an item in your array that is outside the bounds of the array. I.e., if you are trying to access item 6 of an array that only holds 4 items, your app will crash because the array doesn't hold 6 items.