I am stuck in the phoneBook Challenge

I have completed the IOS Foundation course, but I am stuck in the Phonebook challenge.
I have been working on it for a couple of weeks. The solved example was done with somewhat different code style than Chris guidebook example. I have followed Chris forms and style from the Guidebook example, and declared all my variables , created all the company , department and employee cards etc. Finally my code does not have any error but I can’t get it to display anything. if I could post my entire project I would, so someone could tell me what i am doing wrong.

Tanks

Ed

Hi ed,

If you want to share your project then we can take a look to identify where the issue may be.

To compress the project, locate the project folder in Finder and then right click on the top level folder and select Compress ProjectName. The top level folder is the one that contains the file named ProjectName.xcodeproj and the folder named ProjectName.

Upload that zip file to either Dropbox or Google Drive and then create a share link then copy that link and paste that in a reply to this thread.

If you choose to use Google Drive then when you create the Share link, ensure that the “General Access” option is set to “Anyone with the Link” rather than “Restricted”.

Thanks Chris.
Here is the link

Thanks in advance for your help.

Ed

Hi Ed,

This challenge is a bit of a tricky one especially when you have a look at the solution and discover that CodingKeys have been utilised in the Data Model for Company, Department and Employee.

“CodingKeys” I hear you say, “What the heck are they? That was never covered in the course”

…and you are right, they were not covered.

Since the json data does not have an id property for each level we need to tell the JSON decoder to only deal with the properties that exist in the json data file given that we are adding our own id property and assigning it to a UUID().

Have a look at the changes I made to the project and they are:

In the Company model I changed department to departments as the decoder was having a whammy. I also added the CodingKeys.
The change of department to departments also meant that there was a change required in your CompanyView on line 21 which now reads:

departments: company.departments)

and in your DepartmentView where you are passing in an array of departments so it needs to be plural for readability so line 11 is now

@State var departments = [Department]()

and line 15 is now:

ForEach(departments) { department in

In the Department model I added the CodingKeys

In the Employee model I assed the CodingKeys

(by the way I grouped the files in folders to make it a bit easier to find where things are)

In your CompanyView the .onAppear modifier was attached to the NavigationLink inside the ForEach loop. It should be on the upper most View inside the body property which is the NavigationStack.

In your DataService there is also a change in order to correctly parse the json code.

The decoder provides a response which contains an an array of companies so that’s the reason that you have an AppDataResponse model file which has the property companies which is an array of Company. So in your DataService at lines 22 and 23 the code should be something like:

let decodedJson =  try decoder.decode(AppDataResponse.self, from: data)
return decodedJson.companies

With that the program displays data on the screen.

Updated code:

Chris I can’t thank you enough!
You went the extra mile to help me , and a really appreciate it.
I will practice with other projects, reading and decoding json files, before moving to the next course.

Thanks again!

Ed