如何基于泛型类型设置条件断点?

时间:2017-12-12 17:36:27

标签: swift xcode debugging

有没有办法设置断点,如下图所示? enter image description here

我希望在Element类型为ProjectEntity时触发断点,但我收到以下错误:

Stopped due to an error evaluating condition of breakpoint 13.1: "Element == ProjectEntity"
Couldn't parse conditional expression:
error: <EXPR>:3:1: error: use of unresolved identifier 'Element'
Element == ProjectEntity
^~~~~~~

当该断点被击中时,我也无法po Element。知道为什么吗?

2 个答案:

答案 0 :(得分:1)

通常,在swift代码中,您可以在类型后面添加.self以获取表示该类型的Type对象。

所以,

Element.self == ProjectEntity.self

但是,运行时似乎没有识别任何泛型类型参数,所以我想你不能在运行时检查这样的条件。

这意味着您必须获取Element类型的内容,并使用ProjectEntityis进行检查。

someElement is ProjectEntity

答案 1 :(得分:0)

我最终创建了一个分配给泛型类型的局部变量,然后在其上调用type(of:)并根据我想要打破的类型进行检查。

let e = Element.self
let isProject = type(of: e) == ProjectEntity.Type.self

然后我为断点isProject制定了条件。

enter image description here