Xcode 11とSwift 5.1にアップグレードしてから、奇妙な問題が発生しました。オプションの変数がインスタンス化された後も、Xcodeデバッガーでnilとして表示される可能性があります。
私は呼ばれるオプションのクラス変数を持っています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
割り当てられる前に確認できます...それはnilです、素晴らしい:
それが割り当てられた後...何、まだnil ??:
どういうわけか怠惰な変数のように扱われていたのではないかと思いました。ただし、アクセスできるため、実際にはnilではありません。
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.
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.
UPDATE
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"
}
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.
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.
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.
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/?
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変数を含むかなりの数の構造体を使用しているため、今でも回避策を探しています。誰かが見つけたら、下にコメントしてください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加