Reputation: 57511
I'm working with a Swift model Cardholder
which is initialized based on an API response, and I'm trying to figure out what happens if certain fields in the response are null
. Here is a (simplified) extension
of the Cardholder
model with an initializer from a Decoder
:
extension Cardholder: DictionaryDeserializable, DictionarySerializable {
private enum CodingKeys: String, CodingKey {
case id = "id"
case firstName = "first_name"
case lastName = "last_name"
case dateOfBirth = "date_of_birth"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
firstName = try container.decode(String.self, forKey: .firstName)
lastName = try container.decode(String.self, forKey: .lastName)
dateOfBirth = try container.decode(Date.self, forKey: .dateOfBirth)
}
}
For the API response that I'm manually testing with, the first_name
field is null
, and what I'm finding is that the debugger steps straight to the end of the init
method after the firstName =
line:
This happens if I press the 'Step Over' or 'Step In' buttons.
From what I understand rom https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html, this init
method is a throwing function which propagates errors that are thrown inside of it to the scope from which it’s called. How would I get to that scope to figure out what the ultimate consequences are of this error?
Upvotes: 1
Views: 73
Reputation: 299345
Throwing an error in Swift is not an exception. It's just a fancy kind of return. You can step up the calling stack just like you would for return. Step-out; it's the button to the right of step-into.
Upvotes: 3