Swift codable, Default Value to Class property when key missing in the JSON
up vote
2
down vote
favorite
As you know Codable is new stuff in swift 4, So we gonna move to this one from the older initialisation process for the Models. Usually we use the following Scenario
class LoginModal
{
let cashierType: NSNumber
let status: NSNumber
init(_ json: JSON)
{
let keys = Constants.LoginModal()
cashierType = json[keys.cashierType].number ?? 0
status = json[keys.status].number ?? 0
}
}
In the JSON cashierType
Key may missing, so we giving the default Value as 0
Now while doing this with Codable is quite easy, as following
class LoginModal: Coadable
{
let cashierType: NSNumber
let status: NSNumber
}
as mentioned above keys may missing, but we don't want the Model Variables as optional, So How we can achieve this with Codable.
Thanks
ios swift xcode swift4 codable
New contributor
add a comment |
up vote
2
down vote
favorite
As you know Codable is new stuff in swift 4, So we gonna move to this one from the older initialisation process for the Models. Usually we use the following Scenario
class LoginModal
{
let cashierType: NSNumber
let status: NSNumber
init(_ json: JSON)
{
let keys = Constants.LoginModal()
cashierType = json[keys.cashierType].number ?? 0
status = json[keys.status].number ?? 0
}
}
In the JSON cashierType
Key may missing, so we giving the default Value as 0
Now while doing this with Codable is quite easy, as following
class LoginModal: Coadable
{
let cashierType: NSNumber
let status: NSNumber
}
as mentioned above keys may missing, but we don't want the Model Variables as optional, So How we can achieve this with Codable.
Thanks
ios swift xcode swift4 codable
New contributor
First of all don't useNSNumber
in Swift, useInt
,Double
orBool
. Second of all in this case you have to write custom initializer.
– vadian
16 hours ago
@vadian Actually we wants to use codable to manage server response. So as mentioned we are already using custom initialiser, is there any way to handle all this with codable?
– rinku khatri
16 hours ago
You have to implementinit(from decoder:
and usedecodeIfPresent
for the affected properties to be able to assign default values.
– vadian
16 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
As you know Codable is new stuff in swift 4, So we gonna move to this one from the older initialisation process for the Models. Usually we use the following Scenario
class LoginModal
{
let cashierType: NSNumber
let status: NSNumber
init(_ json: JSON)
{
let keys = Constants.LoginModal()
cashierType = json[keys.cashierType].number ?? 0
status = json[keys.status].number ?? 0
}
}
In the JSON cashierType
Key may missing, so we giving the default Value as 0
Now while doing this with Codable is quite easy, as following
class LoginModal: Coadable
{
let cashierType: NSNumber
let status: NSNumber
}
as mentioned above keys may missing, but we don't want the Model Variables as optional, So How we can achieve this with Codable.
Thanks
ios swift xcode swift4 codable
New contributor
As you know Codable is new stuff in swift 4, So we gonna move to this one from the older initialisation process for the Models. Usually we use the following Scenario
class LoginModal
{
let cashierType: NSNumber
let status: NSNumber
init(_ json: JSON)
{
let keys = Constants.LoginModal()
cashierType = json[keys.cashierType].number ?? 0
status = json[keys.status].number ?? 0
}
}
In the JSON cashierType
Key may missing, so we giving the default Value as 0
Now while doing this with Codable is quite easy, as following
class LoginModal: Coadable
{
let cashierType: NSNumber
let status: NSNumber
}
as mentioned above keys may missing, but we don't want the Model Variables as optional, So How we can achieve this with Codable.
Thanks
ios swift xcode swift4 codable
ios swift xcode swift4 codable
New contributor
New contributor
edited 13 hours ago
AS Mackay
1,5993816
1,5993816
New contributor
asked 17 hours ago
rinku khatri
133
133
New contributor
New contributor
First of all don't useNSNumber
in Swift, useInt
,Double
orBool
. Second of all in this case you have to write custom initializer.
– vadian
16 hours ago
@vadian Actually we wants to use codable to manage server response. So as mentioned we are already using custom initialiser, is there any way to handle all this with codable?
– rinku khatri
16 hours ago
You have to implementinit(from decoder:
and usedecodeIfPresent
for the affected properties to be able to assign default values.
– vadian
16 hours ago
add a comment |
First of all don't useNSNumber
in Swift, useInt
,Double
orBool
. Second of all in this case you have to write custom initializer.
– vadian
16 hours ago
@vadian Actually we wants to use codable to manage server response. So as mentioned we are already using custom initialiser, is there any way to handle all this with codable?
– rinku khatri
16 hours ago
You have to implementinit(from decoder:
and usedecodeIfPresent
for the affected properties to be able to assign default values.
– vadian
16 hours ago
First of all don't use
NSNumber
in Swift, use Int
, Double
or Bool
. Second of all in this case you have to write custom initializer.– vadian
16 hours ago
First of all don't use
NSNumber
in Swift, use Int
, Double
or Bool
. Second of all in this case you have to write custom initializer.– vadian
16 hours ago
@vadian Actually we wants to use codable to manage server response. So as mentioned we are already using custom initialiser, is there any way to handle all this with codable?
– rinku khatri
16 hours ago
@vadian Actually we wants to use codable to manage server response. So as mentioned we are already using custom initialiser, is there any way to handle all this with codable?
– rinku khatri
16 hours ago
You have to implement
init(from decoder:
and use decodeIfPresent
for the affected properties to be able to assign default values.– vadian
16 hours ago
You have to implement
init(from decoder:
and use decodeIfPresent
for the affected properties to be able to assign default values.– vadian
16 hours ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Use init(from decoder: Decoder)
to set the default values in your model.
struct LoginModal: Codable {
let cashierType: Int
let status: Int
enum CodingKeys: String, CodingKey {
case cashierType = "cashierType"
case status = "status"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.cashierType = try container.decodeIfPresent(Int.self, forKey: .cashierType) ?? 0
self.status = try container.decodeIfPresent(Int.self, forKey: .status) ?? 0
}
}
Data Reading:
do {
let data = //JSON Data from API
let jsonData = try JSONDecoder().decode(LoginModal.self, from: data)
print("(jsonData.status) (jsonData.cashierType)")
} catch let error {
print(error.localizedDescription)
}
Thanks for the reply. Wants to ask one more thing that can we do this directly with the help of dictionary or firstly have convert it to data?
– rinku khatri
15 hours ago
We should pass the JSON data which is coming from API to JSONDecoder.
– Sateesh
14 hours ago
We are using firebase from there dictionary is coming in response, so can we us it directly or have to convert it in data.
– rinku khatri
14 hours ago
You can format the data from dictionary using JSONSerialization and pass it as an argument to JSONDecoder.
– Sateesh
14 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Use init(from decoder: Decoder)
to set the default values in your model.
struct LoginModal: Codable {
let cashierType: Int
let status: Int
enum CodingKeys: String, CodingKey {
case cashierType = "cashierType"
case status = "status"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.cashierType = try container.decodeIfPresent(Int.self, forKey: .cashierType) ?? 0
self.status = try container.decodeIfPresent(Int.self, forKey: .status) ?? 0
}
}
Data Reading:
do {
let data = //JSON Data from API
let jsonData = try JSONDecoder().decode(LoginModal.self, from: data)
print("(jsonData.status) (jsonData.cashierType)")
} catch let error {
print(error.localizedDescription)
}
Thanks for the reply. Wants to ask one more thing that can we do this directly with the help of dictionary or firstly have convert it to data?
– rinku khatri
15 hours ago
We should pass the JSON data which is coming from API to JSONDecoder.
– Sateesh
14 hours ago
We are using firebase from there dictionary is coming in response, so can we us it directly or have to convert it in data.
– rinku khatri
14 hours ago
You can format the data from dictionary using JSONSerialization and pass it as an argument to JSONDecoder.
– Sateesh
14 hours ago
add a comment |
up vote
1
down vote
accepted
Use init(from decoder: Decoder)
to set the default values in your model.
struct LoginModal: Codable {
let cashierType: Int
let status: Int
enum CodingKeys: String, CodingKey {
case cashierType = "cashierType"
case status = "status"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.cashierType = try container.decodeIfPresent(Int.self, forKey: .cashierType) ?? 0
self.status = try container.decodeIfPresent(Int.self, forKey: .status) ?? 0
}
}
Data Reading:
do {
let data = //JSON Data from API
let jsonData = try JSONDecoder().decode(LoginModal.self, from: data)
print("(jsonData.status) (jsonData.cashierType)")
} catch let error {
print(error.localizedDescription)
}
Thanks for the reply. Wants to ask one more thing that can we do this directly with the help of dictionary or firstly have convert it to data?
– rinku khatri
15 hours ago
We should pass the JSON data which is coming from API to JSONDecoder.
– Sateesh
14 hours ago
We are using firebase from there dictionary is coming in response, so can we us it directly or have to convert it in data.
– rinku khatri
14 hours ago
You can format the data from dictionary using JSONSerialization and pass it as an argument to JSONDecoder.
– Sateesh
14 hours ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Use init(from decoder: Decoder)
to set the default values in your model.
struct LoginModal: Codable {
let cashierType: Int
let status: Int
enum CodingKeys: String, CodingKey {
case cashierType = "cashierType"
case status = "status"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.cashierType = try container.decodeIfPresent(Int.self, forKey: .cashierType) ?? 0
self.status = try container.decodeIfPresent(Int.self, forKey: .status) ?? 0
}
}
Data Reading:
do {
let data = //JSON Data from API
let jsonData = try JSONDecoder().decode(LoginModal.self, from: data)
print("(jsonData.status) (jsonData.cashierType)")
} catch let error {
print(error.localizedDescription)
}
Use init(from decoder: Decoder)
to set the default values in your model.
struct LoginModal: Codable {
let cashierType: Int
let status: Int
enum CodingKeys: String, CodingKey {
case cashierType = "cashierType"
case status = "status"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.cashierType = try container.decodeIfPresent(Int.self, forKey: .cashierType) ?? 0
self.status = try container.decodeIfPresent(Int.self, forKey: .status) ?? 0
}
}
Data Reading:
do {
let data = //JSON Data from API
let jsonData = try JSONDecoder().decode(LoginModal.self, from: data)
print("(jsonData.status) (jsonData.cashierType)")
} catch let error {
print(error.localizedDescription)
}
answered 16 hours ago
Sateesh
1,058313
1,058313
Thanks for the reply. Wants to ask one more thing that can we do this directly with the help of dictionary or firstly have convert it to data?
– rinku khatri
15 hours ago
We should pass the JSON data which is coming from API to JSONDecoder.
– Sateesh
14 hours ago
We are using firebase from there dictionary is coming in response, so can we us it directly or have to convert it in data.
– rinku khatri
14 hours ago
You can format the data from dictionary using JSONSerialization and pass it as an argument to JSONDecoder.
– Sateesh
14 hours ago
add a comment |
Thanks for the reply. Wants to ask one more thing that can we do this directly with the help of dictionary or firstly have convert it to data?
– rinku khatri
15 hours ago
We should pass the JSON data which is coming from API to JSONDecoder.
– Sateesh
14 hours ago
We are using firebase from there dictionary is coming in response, so can we us it directly or have to convert it in data.
– rinku khatri
14 hours ago
You can format the data from dictionary using JSONSerialization and pass it as an argument to JSONDecoder.
– Sateesh
14 hours ago
Thanks for the reply. Wants to ask one more thing that can we do this directly with the help of dictionary or firstly have convert it to data?
– rinku khatri
15 hours ago
Thanks for the reply. Wants to ask one more thing that can we do this directly with the help of dictionary or firstly have convert it to data?
– rinku khatri
15 hours ago
We should pass the JSON data which is coming from API to JSONDecoder.
– Sateesh
14 hours ago
We should pass the JSON data which is coming from API to JSONDecoder.
– Sateesh
14 hours ago
We are using firebase from there dictionary is coming in response, so can we us it directly or have to convert it in data.
– rinku khatri
14 hours ago
We are using firebase from there dictionary is coming in response, so can we us it directly or have to convert it in data.
– rinku khatri
14 hours ago
You can format the data from dictionary using JSONSerialization and pass it as an argument to JSONDecoder.
– Sateesh
14 hours ago
You can format the data from dictionary using JSONSerialization and pass it as an argument to JSONDecoder.
– Sateesh
14 hours ago
add a comment |
rinku khatri is a new contributor. Be nice, and check out our Code of Conduct.
rinku khatri is a new contributor. Be nice, and check out our Code of Conduct.
rinku khatri is a new contributor. Be nice, and check out our Code of Conduct.
rinku khatri is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237085%2fswift-codable-default-value-to-class-property-when-key-missing-in-the-json%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
First of all don't use
NSNumber
in Swift, useInt
,Double
orBool
. Second of all in this case you have to write custom initializer.– vadian
16 hours ago
@vadian Actually we wants to use codable to manage server response. So as mentioned we are already using custom initialiser, is there any way to handle all this with codable?
– rinku khatri
16 hours ago
You have to implement
init(from decoder:
and usedecodeIfPresent
for the affected properties to be able to assign default values.– vadian
16 hours ago