ios - 通过 NSDate 查找数组索引?

标签 ios swift

我想通过 NSDate 找出数组的索引。

我试过了:

var sectionsInTable = [NSDate]()

let indexOfElement = sectionsInTable.indexOf(date) // where date is an NSDate in my sectionsInTable Array

print(indexOfElement)

但我总是得到false

如何从数组中获取 NSDate 的索引?

提前致谢。

最佳答案

如果您有 NSDate 对象的精确副本,您的代码应该可以工作:

let date = NSDate()
let date2 = date.copy() as! NSDate
var sectionsInTable: [NSDate] = [date]

let indexOfElement = sectionsInTable.indexOf(date2)
print(indexOfElement)
//prints: Optional(0)

关于ios - 通过 NSDate 查找数组索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38640497/

相关文章:

ios - 以编程方式设置 UITableView 的背景颜色不起作用

swift - 是否有可能避免 swift 中的结构突变?

ios - 获取已安装相同应用程序的 Twitter 关注者

swift - 导入与Apple Framework同名的pod Framework

ios - Swift:更新 UI - 主线程上的整个功能或只是 UI 更新?

ios - 当应用程序在后台运行或关闭时,Flutter Firebase Messaging 在 IOS 上不起作用

ios - 具有不同项目高度的自下而上的 UITableView 滚动到结束

iphone - 带有调度队列的 iOS5 内存管理

ios - 我如何播放 [UInt8] 音频流?

swift - '非法选项 -- c' 试图从 Swift 运行终端命令