如何检查索引是否是数组中的最后一个

时间:2017-06-27 14:21:43

标签: arrays swift xcode

嘿我正在尝试创建一个遍历整个数组的if语句,然后询问该索引是否是数组中的最后一个。

这是我到目前为止所做的。

代码:

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)  { // Called for each cell made

  if notificationList[indexPath.row] == notificationList.last! {

        print("At last index")

   }
}

更新:

其实我认为这有效,但我会采取其他答案

1 个答案:

答案 0 :(得分:1)

检查如下

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)  { 

  if indexPath.row == notificationList.count - 1 {

        print("At last index")

   }
}