实例化的可选变量在

实例化的可选变量在

本文介绍了实例化的可选变量在 Xcode 调试器中显示为 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我升级到 Xcode 11 和 Swift 5.1 以来,我遇到了一个奇怪的问题——在实例化一个可选变量后,它仍然可以在 Xcode 调试器中显示为 nil!

Since I upgraded to Xcode 11 and Swift 5.1 I've encountered a strange issue -- after an optional variable is instantiated, it can still show as up nil in the Xcode debugger!

我有一个名为 booking 的可选类变量:

I have an optional class variable called booking:

var booking: Booking?

类型为Booking:

public struct Booking: Codable {
    var id: Int?
    var start_time: Date?
    var payment_currency: String = "USD"
    var payment_amount: Int?
}

当我逐步执行代码时,我可以在分配之前看到 booking ......它是零,很棒:

When I'm stepping through the code, I can see booking before it's allocated... It's nil, great:

然后在它被分配之后......什么,仍然为零??:

Then after it's allocated... What, still nil??:

我想知道它是否以某种方式被视为一个惰性变量.但它并不是真正的 nil,因为它可以被访问:

I wondered if it was being treated like a lazy variable somehow. But it's not really nil, because it can be accessed:

搜索了一段时间后,我想知道我在 Xcode 中的构建模式是否没有设置调试可执行文件"标志.但确实如此.我什至在关闭和打开标志的情况下清理和重建项目.

After searching for a while, I wondered if my build schema in Xcode didn't have its "Debug executable" flag set. But it does. I even cleaned and rebuilt the project with the flag off and on to be sure.

无论我在变量视图中查看 booking 还是在控制台视图中输入 p booking,它都显示为 nil.

Whether I view booking in the Variables View or enter p booking in the Console View, it shows up as nil.

这是怎么回事?我需要在这次升级之前我有调试的可预测性.

What's going on here? I need the predictability of debugging I had before this upgrade.

更新

我提炼了一个简单的方法来重现这个问题.首先,创建一个空的单视图项目并将其添加到 AppDelegate.swift 的顶部:

I distilled a simple way to reproduce the issue. First, create an empty single-view project and add this to the top of AppDelegate.swift:

public struct Booking: Codable {
    var start_time: Date?
    var payment_currency: String = "USD"
}

然后将这些行添加到 application(_:didFinishLaunchingWithOptions:) func:

Then add these lines to the application(_:didFinishLaunchingWithOptions:) func:

booking = Booking()
print("booking.payment_currency = (booking?.payment_currency ?? "NULL")")

在运行前和运行时设置断点,注意调试器即使在分配后仍将预订显示为 nil,就像我原来的情况一样.

Set a breakpoint as before and when running, notice that the debugger shows booking as nil even after being assigned, as in my original case.

然后注释掉 start_time 变量,重新运行,并注意到现在调试器显示 booking 在被赋值后有一个值,正如人们所期望的那样.

Then comment out the start_time variable, re-run, and notice that now the debugger shows booking having a value after being assigned, as one would expect.

所以看起来像这样的结构中的日期变量,无论是否可选,都会使调试变得混乱.特别是日期变量——将变量更改为其他类型,如 Int、Int?、String、String?...,没有问题.

So it seems that Date variables, optional or not, in a struct like this make debugging confusing. Specifically Date variables -- change the variable to other types like Int, Int?, String, String?... and there's no issue.

除非我遗漏了一些非常基本的东西,否则对我来说这似乎是 Xcode 调试器中的一个错误.如果是这样,报告它的最佳方式是在 https://developer.apple.com/bug-报告/?

Unless I'm missing something very basic, this seems like a bug in the Xcode debugger to me. If so, would the best way to report it be at https://developer.apple.com/bug-reporting/?

推荐答案

我似乎在 Xcode 11 中发现了一个错误.它很容易重现,如上面 UPDATE 中所述.我已经向 Apple 提交了该错误.

It appears I've found a bug in Xcode 11. It's easily reproducible as outlined in the UPDATE above. I've filed the bug with Apple.

我现在仍在寻找解决方法,因为我使用了很多包含 Date 变量的结构.如果有人能找到,请在下面评论.

I'm still looking for a workaround now, since I use quite a few structs containing Date variables. If anybody can find one, please comment below.

这篇关于实例化的可选变量在 Xcode 调试器中显示为 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 03:28