Decodable nested data without creating additional class in Swift











up vote
3
down vote

favorite












I'm new in iOS development, so sorry for stupid question in advance.
I have json like this:



{
"type":"post",
"comments":{
"count":0,
"can_post":1
},
"likes":{
"count":0,
"user_likes":0,
"can_like":1,
"can_publish":1
},
"reposts":{
"count":0,
"user_reposted":0
}
}


I want to convert this to class which will contain just likesCount, commentsCount, repostsCount but without creating separate classes for comments, likes, reposts. I'm using Decodable for this and here is my code which doesn't work :)



Code:



final class FeedItem: Decodable {
enum Keys: String, CodingKey {
case type,
likes = "likes.count",
comments = "comments.count",
reposts = "reposts.count"
}

let type: String
var likes = 0
var comments = 0
var reposts = 0

required convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
let type = try container.decode(String.self, forKey: .type)
let likes = try container.decode(Int.self, forKey: .likes)
let comments = try container.decode(Int.self, forKey: .comments)
let reposts = try container.decode(Int.self, forKey: .reposts)
self.init(type: type, likes: likes, comments: comments, reposts: reposts)
}

init(type: String,
likes: Int,
comments: Int,
reposts: Int) {
self.type = type
self.likes = likes
self.comments = comments
self.reposts = reposts
}
}


Error:



"No value associated with key Keys(stringValue: "likes.count", intValue: nil) ("likes.count")."









share|improve this question









New contributor




Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use traditional JSONSerialization. It's less effort (and more efficient) than fighting Codable
    – vadian
    Nov 10 at 18:19

















up vote
3
down vote

favorite












I'm new in iOS development, so sorry for stupid question in advance.
I have json like this:



{
"type":"post",
"comments":{
"count":0,
"can_post":1
},
"likes":{
"count":0,
"user_likes":0,
"can_like":1,
"can_publish":1
},
"reposts":{
"count":0,
"user_reposted":0
}
}


I want to convert this to class which will contain just likesCount, commentsCount, repostsCount but without creating separate classes for comments, likes, reposts. I'm using Decodable for this and here is my code which doesn't work :)



Code:



final class FeedItem: Decodable {
enum Keys: String, CodingKey {
case type,
likes = "likes.count",
comments = "comments.count",
reposts = "reposts.count"
}

let type: String
var likes = 0
var comments = 0
var reposts = 0

required convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
let type = try container.decode(String.self, forKey: .type)
let likes = try container.decode(Int.self, forKey: .likes)
let comments = try container.decode(Int.self, forKey: .comments)
let reposts = try container.decode(Int.self, forKey: .reposts)
self.init(type: type, likes: likes, comments: comments, reposts: reposts)
}

init(type: String,
likes: Int,
comments: Int,
reposts: Int) {
self.type = type
self.likes = likes
self.comments = comments
self.reposts = reposts
}
}


Error:



"No value associated with key Keys(stringValue: "likes.count", intValue: nil) ("likes.count")."









share|improve this question









New contributor




Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Use traditional JSONSerialization. It's less effort (and more efficient) than fighting Codable
    – vadian
    Nov 10 at 18:19















up vote
3
down vote

favorite









up vote
3
down vote

favorite











I'm new in iOS development, so sorry for stupid question in advance.
I have json like this:



{
"type":"post",
"comments":{
"count":0,
"can_post":1
},
"likes":{
"count":0,
"user_likes":0,
"can_like":1,
"can_publish":1
},
"reposts":{
"count":0,
"user_reposted":0
}
}


I want to convert this to class which will contain just likesCount, commentsCount, repostsCount but without creating separate classes for comments, likes, reposts. I'm using Decodable for this and here is my code which doesn't work :)



Code:



final class FeedItem: Decodable {
enum Keys: String, CodingKey {
case type,
likes = "likes.count",
comments = "comments.count",
reposts = "reposts.count"
}

let type: String
var likes = 0
var comments = 0
var reposts = 0

required convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
let type = try container.decode(String.self, forKey: .type)
let likes = try container.decode(Int.self, forKey: .likes)
let comments = try container.decode(Int.self, forKey: .comments)
let reposts = try container.decode(Int.self, forKey: .reposts)
self.init(type: type, likes: likes, comments: comments, reposts: reposts)
}

init(type: String,
likes: Int,
comments: Int,
reposts: Int) {
self.type = type
self.likes = likes
self.comments = comments
self.reposts = reposts
}
}


Error:



"No value associated with key Keys(stringValue: "likes.count", intValue: nil) ("likes.count")."









share|improve this question









New contributor




Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm new in iOS development, so sorry for stupid question in advance.
I have json like this:



{
"type":"post",
"comments":{
"count":0,
"can_post":1
},
"likes":{
"count":0,
"user_likes":0,
"can_like":1,
"can_publish":1
},
"reposts":{
"count":0,
"user_reposted":0
}
}


I want to convert this to class which will contain just likesCount, commentsCount, repostsCount but without creating separate classes for comments, likes, reposts. I'm using Decodable for this and here is my code which doesn't work :)



Code:



final class FeedItem: Decodable {
enum Keys: String, CodingKey {
case type,
likes = "likes.count",
comments = "comments.count",
reposts = "reposts.count"
}

let type: String
var likes = 0
var comments = 0
var reposts = 0

required convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
let type = try container.decode(String.self, forKey: .type)
let likes = try container.decode(Int.self, forKey: .likes)
let comments = try container.decode(Int.self, forKey: .comments)
let reposts = try container.decode(Int.self, forKey: .reposts)
self.init(type: type, likes: likes, comments: comments, reposts: reposts)
}

init(type: String,
likes: Int,
comments: Int,
reposts: Int) {
self.type = type
self.likes = likes
self.comments = comments
self.reposts = reposts
}
}


Error:



"No value associated with key Keys(stringValue: "likes.count", intValue: nil) ("likes.count")."






ios json swift decodable






share|improve this question









New contributor




Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 10 at 17:53









Dan Loewenherz

6,37133171




6,37133171






New contributor




Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 10 at 17:29









Magnus Carlsen

183




183




New contributor




Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Magnus Carlsen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Use traditional JSONSerialization. It's less effort (and more efficient) than fighting Codable
    – vadian
    Nov 10 at 18:19




















  • Use traditional JSONSerialization. It's less effort (and more efficient) than fighting Codable
    – vadian
    Nov 10 at 18:19


















Use traditional JSONSerialization. It's less effort (and more efficient) than fighting Codable
– vadian
Nov 10 at 18:19






Use traditional JSONSerialization. It's less effort (and more efficient) than fighting Codable
– vadian
Nov 10 at 18:19














1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










The error is very clear, there are no values for associate keys likes.count as the key not exists.



Use



let container = try decoder.container(keyedBy: CodingKeys.self)


and try container.decodeIfPresent(:_) to check for key exists or if not assign empty value.



Code:



struct FeedItem: Codable {

let type: String
let commentsCount: Int
let canPostComment: Int
let likesCount: Int
let userLikes: Int
let canLike: Int
let canPublish: Int
let repostsCount: Int
let userReposted: Int

enum CodingKeys: String, CodingKey {
case type = "type"
case comments = "comments"
case likes = "likes"
case reposts = "reposts"
case count = "count"
case canPost = "can_post"
case userLikes = "user_likes"
case canLike = "can_like"
case canPublish = "can_publish"
case userReposted = "user_reposted"
}

init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)
self.type = try container.decodeIfPresent(String.self, forKey: .type) ?? ""

let comments = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
self.commentsCount = try comments.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.canPostComment = try comments.decodeIfPresent(Int.self, forKey: .canPost) ?? 0

let likes = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
self.likesCount = try likes.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userLikes = try likes.decodeIfPresent(Int.self, forKey: .userLikes) ?? 0
self.canLike = try likes.decodeIfPresent(Int.self, forKey: .canLike) ?? 0
self.canPublish = try likes.decodeIfPresent(Int.self, forKey: .canPublish) ?? 0

let reposts = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
self.repostsCount = try reposts.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userReposted = try reposts.decodeIfPresent(Int.self, forKey: .userReposted) ?? 0
}

func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)

var comments = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
try comments.encode(commentsCount, forKey: .count)
try comments.encode(canPostComment, forKey: .canPost)

var likes = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
try likes.encode(likesCount, forKey: .count)
try likes.encode(userLikes, forKey: .userLikes)
try likes.encode(canLike, forKey: .canLike)
try likes.encode(canPublish, forKey: .canPublish)

var reposts = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
try reposts.encode(repostsCount, forKey: .count)
try reposts.encode(userReposted, forKey: .userReposted)
}
}


Data Reading:



let data = //Your JSON data from API
let jsonData = try JSONDecoder().decode(FeedItem.self, from: data)
print("(jsonData.type) (jsonData.canLike)")





share|improve this answer























  • I've updated my answer. Please have a look into it.
    – Sateesh
    Nov 10 at 18:11










  • Hi, thanks for you answer, it helped me.
    – Magnus Carlsen
    Nov 10 at 18:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Magnus Carlsen is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241576%2fdecodable-nested-data-without-creating-additional-class-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










The error is very clear, there are no values for associate keys likes.count as the key not exists.



Use



let container = try decoder.container(keyedBy: CodingKeys.self)


and try container.decodeIfPresent(:_) to check for key exists or if not assign empty value.



Code:



struct FeedItem: Codable {

let type: String
let commentsCount: Int
let canPostComment: Int
let likesCount: Int
let userLikes: Int
let canLike: Int
let canPublish: Int
let repostsCount: Int
let userReposted: Int

enum CodingKeys: String, CodingKey {
case type = "type"
case comments = "comments"
case likes = "likes"
case reposts = "reposts"
case count = "count"
case canPost = "can_post"
case userLikes = "user_likes"
case canLike = "can_like"
case canPublish = "can_publish"
case userReposted = "user_reposted"
}

init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)
self.type = try container.decodeIfPresent(String.self, forKey: .type) ?? ""

let comments = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
self.commentsCount = try comments.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.canPostComment = try comments.decodeIfPresent(Int.self, forKey: .canPost) ?? 0

let likes = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
self.likesCount = try likes.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userLikes = try likes.decodeIfPresent(Int.self, forKey: .userLikes) ?? 0
self.canLike = try likes.decodeIfPresent(Int.self, forKey: .canLike) ?? 0
self.canPublish = try likes.decodeIfPresent(Int.self, forKey: .canPublish) ?? 0

let reposts = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
self.repostsCount = try reposts.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userReposted = try reposts.decodeIfPresent(Int.self, forKey: .userReposted) ?? 0
}

func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)

var comments = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
try comments.encode(commentsCount, forKey: .count)
try comments.encode(canPostComment, forKey: .canPost)

var likes = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
try likes.encode(likesCount, forKey: .count)
try likes.encode(userLikes, forKey: .userLikes)
try likes.encode(canLike, forKey: .canLike)
try likes.encode(canPublish, forKey: .canPublish)

var reposts = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
try reposts.encode(repostsCount, forKey: .count)
try reposts.encode(userReposted, forKey: .userReposted)
}
}


Data Reading:



let data = //Your JSON data from API
let jsonData = try JSONDecoder().decode(FeedItem.self, from: data)
print("(jsonData.type) (jsonData.canLike)")





share|improve this answer























  • I've updated my answer. Please have a look into it.
    – Sateesh
    Nov 10 at 18:11










  • Hi, thanks for you answer, it helped me.
    – Magnus Carlsen
    Nov 10 at 18:26















up vote
2
down vote



accepted










The error is very clear, there are no values for associate keys likes.count as the key not exists.



Use



let container = try decoder.container(keyedBy: CodingKeys.self)


and try container.decodeIfPresent(:_) to check for key exists or if not assign empty value.



Code:



struct FeedItem: Codable {

let type: String
let commentsCount: Int
let canPostComment: Int
let likesCount: Int
let userLikes: Int
let canLike: Int
let canPublish: Int
let repostsCount: Int
let userReposted: Int

enum CodingKeys: String, CodingKey {
case type = "type"
case comments = "comments"
case likes = "likes"
case reposts = "reposts"
case count = "count"
case canPost = "can_post"
case userLikes = "user_likes"
case canLike = "can_like"
case canPublish = "can_publish"
case userReposted = "user_reposted"
}

init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)
self.type = try container.decodeIfPresent(String.self, forKey: .type) ?? ""

let comments = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
self.commentsCount = try comments.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.canPostComment = try comments.decodeIfPresent(Int.self, forKey: .canPost) ?? 0

let likes = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
self.likesCount = try likes.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userLikes = try likes.decodeIfPresent(Int.self, forKey: .userLikes) ?? 0
self.canLike = try likes.decodeIfPresent(Int.self, forKey: .canLike) ?? 0
self.canPublish = try likes.decodeIfPresent(Int.self, forKey: .canPublish) ?? 0

let reposts = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
self.repostsCount = try reposts.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userReposted = try reposts.decodeIfPresent(Int.self, forKey: .userReposted) ?? 0
}

func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)

var comments = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
try comments.encode(commentsCount, forKey: .count)
try comments.encode(canPostComment, forKey: .canPost)

var likes = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
try likes.encode(likesCount, forKey: .count)
try likes.encode(userLikes, forKey: .userLikes)
try likes.encode(canLike, forKey: .canLike)
try likes.encode(canPublish, forKey: .canPublish)

var reposts = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
try reposts.encode(repostsCount, forKey: .count)
try reposts.encode(userReposted, forKey: .userReposted)
}
}


Data Reading:



let data = //Your JSON data from API
let jsonData = try JSONDecoder().decode(FeedItem.self, from: data)
print("(jsonData.type) (jsonData.canLike)")





share|improve this answer























  • I've updated my answer. Please have a look into it.
    – Sateesh
    Nov 10 at 18:11










  • Hi, thanks for you answer, it helped me.
    – Magnus Carlsen
    Nov 10 at 18:26













up vote
2
down vote



accepted







up vote
2
down vote



accepted






The error is very clear, there are no values for associate keys likes.count as the key not exists.



Use



let container = try decoder.container(keyedBy: CodingKeys.self)


and try container.decodeIfPresent(:_) to check for key exists or if not assign empty value.



Code:



struct FeedItem: Codable {

let type: String
let commentsCount: Int
let canPostComment: Int
let likesCount: Int
let userLikes: Int
let canLike: Int
let canPublish: Int
let repostsCount: Int
let userReposted: Int

enum CodingKeys: String, CodingKey {
case type = "type"
case comments = "comments"
case likes = "likes"
case reposts = "reposts"
case count = "count"
case canPost = "can_post"
case userLikes = "user_likes"
case canLike = "can_like"
case canPublish = "can_publish"
case userReposted = "user_reposted"
}

init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)
self.type = try container.decodeIfPresent(String.self, forKey: .type) ?? ""

let comments = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
self.commentsCount = try comments.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.canPostComment = try comments.decodeIfPresent(Int.self, forKey: .canPost) ?? 0

let likes = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
self.likesCount = try likes.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userLikes = try likes.decodeIfPresent(Int.self, forKey: .userLikes) ?? 0
self.canLike = try likes.decodeIfPresent(Int.self, forKey: .canLike) ?? 0
self.canPublish = try likes.decodeIfPresent(Int.self, forKey: .canPublish) ?? 0

let reposts = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
self.repostsCount = try reposts.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userReposted = try reposts.decodeIfPresent(Int.self, forKey: .userReposted) ?? 0
}

func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)

var comments = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
try comments.encode(commentsCount, forKey: .count)
try comments.encode(canPostComment, forKey: .canPost)

var likes = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
try likes.encode(likesCount, forKey: .count)
try likes.encode(userLikes, forKey: .userLikes)
try likes.encode(canLike, forKey: .canLike)
try likes.encode(canPublish, forKey: .canPublish)

var reposts = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
try reposts.encode(repostsCount, forKey: .count)
try reposts.encode(userReposted, forKey: .userReposted)
}
}


Data Reading:



let data = //Your JSON data from API
let jsonData = try JSONDecoder().decode(FeedItem.self, from: data)
print("(jsonData.type) (jsonData.canLike)")





share|improve this answer














The error is very clear, there are no values for associate keys likes.count as the key not exists.



Use



let container = try decoder.container(keyedBy: CodingKeys.self)


and try container.decodeIfPresent(:_) to check for key exists or if not assign empty value.



Code:



struct FeedItem: Codable {

let type: String
let commentsCount: Int
let canPostComment: Int
let likesCount: Int
let userLikes: Int
let canLike: Int
let canPublish: Int
let repostsCount: Int
let userReposted: Int

enum CodingKeys: String, CodingKey {
case type = "type"
case comments = "comments"
case likes = "likes"
case reposts = "reposts"
case count = "count"
case canPost = "can_post"
case userLikes = "user_likes"
case canLike = "can_like"
case canPublish = "can_publish"
case userReposted = "user_reposted"
}

init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)
self.type = try container.decodeIfPresent(String.self, forKey: .type) ?? ""

let comments = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
self.commentsCount = try comments.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.canPostComment = try comments.decodeIfPresent(Int.self, forKey: .canPost) ?? 0

let likes = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
self.likesCount = try likes.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userLikes = try likes.decodeIfPresent(Int.self, forKey: .userLikes) ?? 0
self.canLike = try likes.decodeIfPresent(Int.self, forKey: .canLike) ?? 0
self.canPublish = try likes.decodeIfPresent(Int.self, forKey: .canPublish) ?? 0

let reposts = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
self.repostsCount = try reposts.decodeIfPresent(Int.self, forKey: .count) ?? 0
self.userReposted = try reposts.decodeIfPresent(Int.self, forKey: .userReposted) ?? 0
}

func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)

var comments = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .comments)
try comments.encode(commentsCount, forKey: .count)
try comments.encode(canPostComment, forKey: .canPost)

var likes = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .likes)
try likes.encode(likesCount, forKey: .count)
try likes.encode(userLikes, forKey: .userLikes)
try likes.encode(canLike, forKey: .canLike)
try likes.encode(canPublish, forKey: .canPublish)

var reposts = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .reposts)
try reposts.encode(repostsCount, forKey: .count)
try reposts.encode(userReposted, forKey: .userReposted)
}
}


Data Reading:



let data = //Your JSON data from API
let jsonData = try JSONDecoder().decode(FeedItem.self, from: data)
print("(jsonData.type) (jsonData.canLike)")






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 18:10

























answered Nov 10 at 17:51









Sateesh

1,216514




1,216514












  • I've updated my answer. Please have a look into it.
    – Sateesh
    Nov 10 at 18:11










  • Hi, thanks for you answer, it helped me.
    – Magnus Carlsen
    Nov 10 at 18:26


















  • I've updated my answer. Please have a look into it.
    – Sateesh
    Nov 10 at 18:11










  • Hi, thanks for you answer, it helped me.
    – Magnus Carlsen
    Nov 10 at 18:26
















I've updated my answer. Please have a look into it.
– Sateesh
Nov 10 at 18:11




I've updated my answer. Please have a look into it.
– Sateesh
Nov 10 at 18:11












Hi, thanks for you answer, it helped me.
– Magnus Carlsen
Nov 10 at 18:26




Hi, thanks for you answer, it helped me.
– Magnus Carlsen
Nov 10 at 18:26










Magnus Carlsen is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Magnus Carlsen is a new contributor. Be nice, and check out our Code of Conduct.













Magnus Carlsen is a new contributor. Be nice, and check out our Code of Conduct.












Magnus Carlsen is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241576%2fdecodable-nested-data-without-creating-additional-class-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python