fatal error: Index out of range

前端 未结 4 2102
长发绾君心
长发绾君心 2021-02-07 11:33

I want to show 25 of the songs I have in my library. This is my code:

var allSongsArray: [MPMediaItem] = []
let songsQuery = MPMediaQuery.songsQuery()

func numb         


        
4条回答
  •  星月不相逢
    2021-02-07 12:04

    In case your app crashes in

     let items = allSongsArray[indexPath.row]
    

    as you check if the allSongsArray.count, u can safe guard it by making sure that the [indexPath.row] doesn't exceed your array count. so u can write a simple if condition as;

    if allSongsArray.count > 0 && indexPath.row < allSongsArray.count {
    
      //Do the needful
    
    }
    

提交回复
热议问题