【问题标题】:How to set the condition of conditional breakpoints in Swift with self and enum?如何使用 self 和 enum 在 Swift 中设置条件断点的条件?
【发布时间】:2015-07-03 23:22:59
【问题描述】:

我有一个 HTTP 方法的枚举:

enum HTTPMethod: String {
  case GET = "GET"
  case POST = "POST"
}

我有一个请求类和一个请求包装类:

class Request {
  let method: HTTPMethod = .GET
}

class RequestWrapper {
  let request: Request

  func compareToRequest(incomingRequest: NSURLRequest) -> Bool {

     // Next line is where the conditional breakpoint set.
     return request.method.rawValue == incomingRequest.HTTPMethod
  }
}

我在行上设置了条件断点:

return request.method.rawValue == incomingRequest.HTTPMethod

有条件:

self.request.method == HTTPMethod.POST

然后调试器停在错误信息行:

Stopped due to an error evaluating condition of breakpoint 1.1:     
"self.request.method == HTTPMethod.POST"
Couldn't parse conditional expression:
<EXPR>:1:1: error: use of unresolved identifier 'self'
self.request.HTTPMethod == HTTPMethod.POST

如果我删除 self 并将条件更改为:

request.method == HTTPMethod.POST

错误信息如下:

Stopped due to an error evaluating condition of breakpoint 1.1:  
"request.method == HTTPMethod.POST"
Couldn't parse conditional expression:
<EXPR>:1:1: error: could not find member 'method'
request.method == HTTPMethod.POST
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

有没有办法解决这个问题?

更新:

可以使用 LLDB 命令检查self.request.method 的值:

fr v self.request.method

如果我使用本地常量来存储值,调试器可以在正确的位置停止:

// Use a local constant to store the HTTP method
let method = request.method

// Condition of breakpoint
method == HTTPMethod.POST

更新 2:

我使用的是 Xcode 6.3.1

【问题讨论】:

    标签: ios swift debugging enums lldb


    【解决方案1】:

    这很明显是一个 lldb 错误。您没有提及您使用的是什么版本的工具。如果您没有使用 6.3.1 或更高版本,请再次尝试使用该版本。如果您仍然发现问题,请通过http://bugreporter.apple.com 提交错误。

    注意,frame varexpr 是完全不同的野兽。 frame var 仅打印局部变量的值,直接使用 DWARF 调试信息,但不是表达式评估器。例如,它对== 的了解不够。我想如果你这样做了:

    (lldb) self.request.method == HTTPMethod.POST
    

    在该断点处停止时,您会看到相同的效果。

    表达式解析器必须发挥额外的技巧,即伪装成你的类的方法(通过 self 等获得透明引用才能工作),而且要做到这一点有点棘手。显然,在您的情况下,我们没有正确地完成这项工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      相关资源
      最近更新 更多