问题
I have model as below.
struc Info: Decodable {
var firstName: String?
var lastName: String?
}
While displaying in tableview cell, what I am doing is as below.
personName.text = "\(personArray[indexPath.row].firstName!) \(personArray[indexPath.row].lastName!)"
Now the app is crashing if I have data in below format
[
{
"firstName" : "F 1",
"lastName" : "L 1"
},
{
"firstName" : "F 2"
},
{
"lastName" : "L 3"
}
]
The app is crashing saying lastName is nil
Solution 1
The solution for this check for nil & then show name, however I don't want to do the check at run time because that I have to check this for all variables (considering I have model of 25 variables). Below is what I could have done.
var firstName = ""
if (personArray[indexPath.row].firstName == nil) {
firstName = ""
} else {
firstName = personArray[indexPath.row].firstName!
}
var lastName = ""
if (personArray[indexPath.row].lastName == nil) {
lastName = ""
} else {
lastName = personArray[indexPath.row].lastName!
}
personName.text = "\(firstName) \(lastName)"
Solution 2
I can do the update in the model itself as below.
struc Info: Decodable {
var firstName: String?
var lastName: String?
var firstName2 : String? {
get {
if (self.firstName==nil) {
return ""
}
return firstName
}
var lastName2 : String? {
get {
if (self.lastName==nil) {
return ""
}
return lastName
}
}
personName.text = "\(personArray[indexPath.row].firstName2!) \(personArray[indexPath.row].lastName2!)"
However I have problem with this also. This way, again I have to create N number of variables again.
Is there any other alternate way where default value will get assigned if that variable is missing in the webservice?
回答1:
I would recommend one of two options:
- Add computed property to the struct to determine the display name.
- Manually decode, providing default values. (And also add a display name property if you want)
Personally, I like option 1. I think it's the most compact and also the easiest to maintain.
Option 1 Example:
struct Info1: Decodable {
var firstName: String?
var lastName: String?
var displayName: String {
return [self.firstName, self.lastName]
.compactMap { $0 } // Ignore 'nil'
.joined(separator: " ") // Combine with a space
}
}
print(Info1(firstName: "John", lastName: "Smith").displayName)
// Output: "John Smith"
print(Info1(firstName: "John", lastName: nil).displayName)
// Output: "John"
print(Info1(firstName: nil, lastName: "Smith").displayName)
// Output: "Smith"
print(Info1(firstName: nil, lastName: nil).displayName)
// Output: ""
Option 2 Example:
struct Info2: Decodable {
var firstName: String
var lastName: String
enum CodingKeys: String, CodingKey {
case firstName, lastName
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.firstName = try container.decodeIfPresent(String.self, forKey: .firstName) ?? ""
self.lastName = try container.decodeIfPresent(String.self, forKey: .lastName) ?? ""
}
// Optional:
var displayName: String {
return [self.firstName, self.lastName]
.compactMap { $0.isEmpty ? nil : $0 } // Ignore empty strings
.joined(separator: " ") // Combine with a space
}
// TEST:
init(from dict: [String: Any]) {
let data = try! JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
self = try! JSONDecoder().decode(Info2.self, from: data)
}
}
print(Info2(from: ["firstName": "John", "lastName": "Smith"]).displayName)
// Output: "John Smith"
print(Info2(from: ["lastName": "Smith"]).displayName)
// Output: "Smith"
print(Info2(from: ["firstName": "John"]).displayName)
// Output: "John"
print(Info2(from: [String: Any]()).displayName)
// Output: ""
来源:https://stackoverflow.com/questions/50566478/how-to-handle-the-nil-value-variables