我想通过 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/