-2

I have an app that locally prints out all of a users songs in a list.

This is the array:

var mySongs: [MPMediaItem] = []

This is how the songs are fetched:

self.mySongs = MPMediaQuery.songs().items ?? []

I want to loop through the mySongs array and add specific songs from it into newArray

for songs in mySongs{
        if songs.title == "Actin Crazy"{
            newArray[counter] = songs
            counter+=1
        }
    }

But I am getting this error: Thread 1: Fatal error: Index out of range

How do I figure out how/where its going out of range in order to fix this?

ctm
  • 88
  • 1
  • 1
  • 10
  • What exactly is “the hump”? Are you aware that the `title` is an Optional? Do you know what an Optional is? – matt Apr 01 '21 at 04:18
  • @matt The title is there just for the example condition where in this case I would move a song to the array if it had the name I was looking for. The hump for me was a bunch of errors that were being thrown my way. I'm trying the below right now though and not getting any problems, what are your thoughts? for songs in mySongs{ if songs.title == "Wonderland"{ newArray[counter] = songs counter+=1 } } – ctm Apr 01 '21 at 04:35
  • Put real code in your question, give real error messages, ask specifically about what troubles you. – matt Apr 01 '21 at 04:40
  • @matt Post updated, please let me know if this is better and better equipped for Stackoverflow. – ctm Apr 01 '21 at 04:49
  • 1
    To add an item to the end of an array use `newArray.append(songs)` - You can't just refer to an index that doesn't exist, – Paulw11 Apr 01 '21 at 05:02
  • 1
    Like @Paulw11 already said just try this instead of looping: `let mySongs = ["MPMediaItem] var newSongs: [String] = [] if let match = mySongs.first(where: { $0.title == "a" }) { newSongs.append(match) } print(newSongs)` – LoVo Apr 01 '21 at 05:13
  • @Paulw11 This worked perfectly, thank you so much for educating me. – ctm Apr 01 '21 at 05:33
  • @LoVo wow! I had no idea I could do this instead of looping. Thank you for the enlightenment this makes sense. – ctm Apr 01 '21 at 06:17

1 Answers1

1

You cannot append to an array by saying

newArray[counter] = ...

To see why, suppose the array is empty. Then you would be saying

newArray[0] = ...

But the array is empty. There is no newArray[0]! Therefore you will crash when app runs — with an out of range error! It is illegal at any time to subscript an array by a nonexistent index.

Instead, use newArray.append(...). That is why it exists.

However, your code has many other issues. In particular the title of an MPMediaItem is an Optional. You would need to unwrap it safely to use it.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you deeply for your answer Matt, and also for your critique on my question which will help me become better at using this site! My code is running perfectly. I will also look into how I can go about unwrapping safely the optional so I can avoid any issues. – ctm Apr 02 '21 at 18:18