实例化的可选变量在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??:
我想知道是否以某种方式将其视为惰性变量.但这并不是真正的零,因为它可以被访问:
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"
}
然后将这些行添加到应用程序(_:didFinishLaunchingWithOptions :) func:
Then add these lines to the application(_:didFinishLaunchingWithOptions:) func:
booking = Booking()
print("booking.payment_currency = \(booking?.payment_currency ?? "NULL")")
在运行之前和运行时设置一个断点,请注意,即使在分配调试器后,调试器也显示预订为零,就像我原来的情况一样.
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.
因此,像这样的结构中的Date变量(无论是否可选)似乎使调试令人困惑.特别是Date变量-将变量更改为其他类型,例如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.