我正在使用 ios 库 Charts ( https://github.com/danielgindi/Charts )
声明了一个委托函数chartValueSelected
,这给了我回报entry: ChartDataEntry
。
所以我得到entry
, with it's data
, 声明为var data: AnyObject?
print(entry.data)
> Optional(Optional(<MyProject.Stop: 0x6000002c5cc0> (entity: Stop; id: 0xd00000000c980010 <x-coredata://5F54CCEC-11FB-42F1-BDFE-30F7F7E18614/Stop/p806> ; data: { ... })))
print(type(of: entry.data))
> Optional<AnyObject>
这很奇怪吗?我给它分配了一个非可选的?好吧,这可能是库中的错误,但我至少应该能够访问它?
guard let e = stopEntry as? Stop else {
continue
}
yVal.append(BarChartDataEntry(value: e.duration!.doubleValue, xIndex: i, data: e))
好吧,我们对??
双选项很好。但是什么?为什么我们不能打开它?
if let hasObject = entry.data {
print("We have object: \(type(of: hasObject))")
> We have object: _SwiftValue
if let stopObject = hasObject as? Stop {
print("We have a stop object!!") // Doesnt come here
}
}
更多不起作用的事情是:
if let s = entry.data, let b = s as? Stop {
// Not executed here either
}