【问题标题】:How to check if index is the last one in the array如何检查索引是否是数组中的最后一个
【发布时间】:2017-06-27 14:21:43
【问题描述】:

嘿,我正在尝试创建一个遍历整个数组的 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")

   }
}

更新:

其实我认为这可行,但我会接受其他答案

【问题讨论】:

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

标签: arrays swift xcode


【解决方案1】:

检查如下

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

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

        print("At last index")

   }
}

【讨论】:

  • 如果我能让它正常工作,它会因某种原因无法正常工作
猜你喜欢
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 2016-02-20
  • 2015-08-26
相关资源
最近更新 更多