Learn Courses My Dashboard

Ios foundations module 4 DataService error

When making the update to the json data for the RecipeListApp i got an error- Value of type ‘String’ has no member ‘id’- However i wrote my class exactly as the lesson instructed.

I have put the code I used in a gist above

@JohanneN

Hi Johanne,

Welcome to the code crew community.

Post all of your DataService code in a reply so that we can see the complete context.

Paste your code in as text, rather than providing a screenshot.

To format the code nicely, place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. Like this:

```
Code goes here
```

The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key). You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

Here is the rest of my DataService code

import Foundation

class DataService {
    
    
    
   static func getLocalData() -> [Recipe] {
        
       
        // Parse local json file
        
        // Get a url path to the json file
        let pathString = Bundle.main.path(forResource: "recipes", ofType: "json")
        
        // Check if pathString is not nil, otherwise...
        guard pathString != nil else {
            return [Recipe]()
        }
       
        // Create a url object
        let url = URL(fileURLWithPath: pathString!)
    
        do {
            // create a data object
            let data = try Data(contentsOf: url)
            
            // decode the data with a json decoder
            let decoder = JSONDecoder()
            
            do {
                let recipeData = try decoder.decode([Recipe].self, from: data)
                // add the unique IDs
                for r in recipeData {
                    r.id = UUID()
                    
                    for i in r.ingredients{
                        i.id = UUID()
                    }
                    
                }
                // Return the recipes
                return recipeData
            }
            catch {
                // error with parsing json
                print(error)
            }
        }
        catch {
            print(error)
        }
        
     return [Recipe]()
    }
}

Thanks for the reply!

Hmmmm, that should work.

Maybe try cleaning your Build Folder - Shift + Command + K

Cleaning the build folder cleared the error

Thanks for the help Chris!

1 Like