# JSON Decoding | raywenderlich.com

**URL:** <https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357>\
**Category:** 🎓 Courses & Articles\
**Created:** [April 29, 2020, 8:41pm UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357 "2020-04-29T20:41:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![airjordan12345](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/airjordan12345/32/93546_2.png) [@airjordan12345](https://forums.kodeco.com/u/airjordan12345)\
**Post date:** [April 29, 2020, 8:41pm UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/1 "2020-04-29T20:41:48Z")

</div>

* * *
This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/5429634-saving-data-in-ios/lessons/11](https://www.raywenderlich.com/5429634-saving-data-in-ios/lessons/11)

---

<div class="post-metadata">

**Author:** ![nz\_hamza](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nz_hamza/32/81494_2.png) [@nz\_hamza](https://forums.kodeco.com/u/nz_hamza)\
**Post date:** [May 5, 2020, 4:37am UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/2 "2020-05-05T04:37:43Z")

</div>

When we change the PrioritizedTask to coform to Codable protocol we still saw an error. In the tutorial video once we change the Priority enum to also be Codable the issue resolved.

My question is, how do we know that it needs to conform to Codable protocol as well. Because the error that we gave was also not informative enough to identify this change.

---

<div class="post-metadata">

**Author:** ![airjordan12345](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/airjordan12345/32/93546_2.png) [@airjordan12345](https://forums.kodeco.com/u/airjordan12345)\
**Post date:** [May 5, 2020, 2:34pm UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/3 "2020-05-05T14:34:34Z")

</div>

Excellent question! Thank you asking it 🙂

A good general rule would be to ensure that all of your properties, and their respective types, can be encoded and, thus, adhere to Codable. A lot of built in types already do this, like `Int`, `String`, etc.

However, let’s think of the following example:

```auto
struct Reminder {
  var creator: User
  var date: Date
  var text: String
}

struct User {
  var name: String
  var age: Int
}

```

Now let’s say you just want to save your reminders, so you make it adhere to Codable:

```auto
struct Reminder: Codable {
  var creator: User
  var date: Date
  var text: String
}

```

You are going to get that error you mention, and the reason is that if you go down the list of properties in your custom `Reminder` type, one by one, you need to ask yourself: “Can this be encoded automatically?”.

In the case of `date` and `text` the answer is yes, but in the case of `creator`, Swift is going to say "I have no idea what the `User` type is. That becomes your indicator to look in `User` and have it conform to `Codable`:

```auto
struct User: Codable {
  var name: String
  var age: Int
}

```

Same as with `Reminder`, go through your properties one by one and ask whether they can be encoded out of the box. Both `String` and `Int` are `Codable` types, and now `User` conforms to `Codable`, so all of your errors should be gone.

Think of it like a tree with many branches, where each branch is a property with a type. All branches in the tree must be “codable” or the entire tree will fail to encode 🙂

Let me know if that helps clarify things for you and if it helps you for future types you may encode/decode 😃

---

<div class="post-metadata">

**Author:** ![shrutisharma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shrutisharma/32/81494_2.png) [@shrutisharma](https://forums.kodeco.com/u/shrutisharma)\
**Post date:** [July 14, 2020, 11:56pm UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/4 "2020-07-14T23:56:15Z")

</div>

Excellent explanation of the above question !

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 15, 2020, 5:32am UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/5 "2020-07-15T05:32:58Z")

</div>

@nz_hamza @shrutisharma Please also check out our encoding and decoding tutorial when you get a chance:

[https://www.raywenderlich.com/3418439-encoding-and-decoding-in-swift](https://www.raywenderlich.com/3418439-encoding-and-decoding-in-swift)

I hope it helps!

---

<div class="post-metadata">

**Author:** ![wtj123](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/wtj123/32/102075_2.png) [@wtj123](https://forums.kodeco.com/u/wtj123)\
**Post date:** [August 30, 2020, 3:35pm UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/6 "2020-08-30T15:35:17Z")

</div>

In the ContentView, the code  
ForEach(taskStore.prioritizedTasks) { index in  
…. }  
specifies the type of the element of the prioritizedTasks is PrioritizedTasks. However, the the type of the parameter of the closure is Int. Why can this setting still work and is no error shown?

---

<div class="post-metadata">

**Author:** ![airjordan12345](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/airjordan12345/32/93546_2.png) [@airjordan12345](https://forums.kodeco.com/u/airjordan12345)\
**Post date:** [August 31, 2020, 11:03pm UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/7 "2020-08-31T23:03:46Z")

</div>

In this case we are asking SwiftUI to help us create some Views for a given number of items in our collection. The closure parameter, however, is the `index` of each item in `prioritizedTasks`.

Let’s say we have 5 prioritized tasks that we loop through using `ForEach`. It will go through them and tell us: “Hey, I’m currently working with a `prioritizedTask` that’s at index 2 of the collection”. This allows us to do things specifically per index, or anything we need necessary.

There are a couple of flavors of `ForEach` available for us, we decided to go with this one for this particular example 🙂

Hope that helps clear up a bit why we have one type of parameter for the closure versus the actual collection we use in the `ForEach`.

---

<div class="post-metadata">

**Author:** ![wtj123](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/wtj123/32/102075_2.png) [@wtj123](https://forums.kodeco.com/u/wtj123)\
**Post date:** [September 1, 2020, 1:20am UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/8 "2020-09-01T01:20:59Z")

</div>

First of all, thank you very much for your detailed and practical course, which helped me a lot. At the same time, thank you for your immediate response.

I still have some questions I want to discuss with you again.  
After checking Developer Documentation a few times, I found that ForEach has three initializers:  
init(Data, content: (Data.Element) → Content)  
init(Range, content: (Int) → Content)  
init(Data, id: KeyPath\<Data.Element, ID\>, content: (Data.Element) → Content)

It seems that the parameter type of the closure must be the same as Data.Element.

For testing, I created a new file and reprogrammed it according to your sample program.  
When compiling, the following error appears:  
Cannot convert value of type’[PrioritizedTasks]’ to expected argument type’Range Int\>’

Do you have any suggestions for dealing with this problem?

---

<div class="post-metadata">

**Author:** ![airjordan12345](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/airjordan12345/32/93546_2.png) [@airjordan12345](https://forums.kodeco.com/u/airjordan12345)\
**Post date:** [September 1, 2020, 1:35am UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/9 "2020-09-01T01:35:22Z")

</div>

My most sincere apology. I forgot to mention that the initializer we are using there is an extension we created in `ForEach.swift`:

```auto
  init<Base: RandomAccessCollection>(
    _ base: Base,
    @ViewBuilder content: @escaping (Base.Index) -> Content
  )
  where
    Data == IndexedCollection<Base>,
    Base.Element: Identifiable,
    ID == Base.Element.ID
  {
    self.init(IndexedCollection(base), id: \.element.id) {
      index, _ in content(index)
    }
  }

```

This allows us to iterate over a collection and have the index of the item being iterated on as a closure parameter 🙂

Hopefully that helps clarify why it works so different than the three (at least last I checked 😛 ) initializer we get out-of-the-box for `ForEach` in SwiftUI 🙂

---

<div class="post-metadata">

**Author:** ![wtj123](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/wtj123/32/102075_2.png) [@wtj123](https://forums.kodeco.com/u/wtj123)\
**Post date:** [September 1, 2020, 4:21am UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/10 "2020-09-01T04:21:59Z")

</div>

Thank you for your explanation.  
You provided a great custom initializer, and I will use it often in the future.

---

<div class="post-metadata">

**Author:** ![airjordan12345](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/airjordan12345/32/93546_2.png) [@airjordan12345](https://forums.kodeco.com/u/airjordan12345)\
**Post date:** [October 31, 2020, 1:11am UTC](https://forums.kodeco.com/t/json-decoding-raywenderlich-com/107357/11 "2020-10-31T01:11:28Z")

</div>

You’re most welcome. Apologies for replying after several weeks.

I hope you are enjoying the course, and feel free to ask any questions you may have 🙂
