fatal error: Index out of range

前端 未结 4 2095
长发绾君心
长发绾君心 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 11:47

    When you first create the array it is empty. Hence, it will give you out of bound error.

    Try to return the count of the songs array instead.

    1st you need to get the data into the array and then update the table view.

    Here is a sample code:

    @IBAction private func refresh(sender: UIRefreshControl?) {
            if myArray.count > 0 {
                  self.tableView.reloadData()
                  sender?.endRefreshing()
            } else {
                  sender?.endRefreshing()
            }
        }
    

提交回复
热议问题