【发布时间】:2019-02-26 07:20:15
【问题描述】:
我有如下功能:
func fetchComment(postId: String, index: Int, callback: @escaping (_ comment: Comment) -> Void) {
rtDB!.fetchComment(postId: postId, callback: { comment in
self.log.debug("postId: \(postId) - comment: \(comment)")
self.fetchUsersForComments([postId: comment], callback: { usersList in
// self.log.debug("++++ postId: \(postId) - comment: \(comment)") // 1
self.log.debug("postId: \(postId) - usersList: \(usersList)")
})
})
}
如果我在 1 添加断点并注释掉该行并打印 p comment,我会收到未定义标识符 comment 消息。但是comment 作为闭包参数传递给fetchComment 方法。但是,如果我取消注释标记为1 的行,它使用comment 变量,然后用断点打印p comment,它工作正常。如果 comment 变量没有在内部范围内使用,为什么没有定义它?是 Swift 编译器进行优化并删除变量吗?我没有为调试更多启用优化。
【问题讨论】:
标签: ios swift closures breakpoints