Learn Courses My Dashboard

Problem with Decodable protocol when use in Class

when i add a Decodable protocol to the Class . It is showing error !
But it works on struct .

this is it shown title - [ type ‘business’ does not conform to protocol ‘decodable’ ]

How i can fix this problems ??

My os : macos 12.1
Xcode 13.1
City Sight App _ Module 6_ lesson 6-7-8

// FIRST  ATTEMP

import Foundation

class Business: Identifiable, ObservableObject , Decodable {
 
    
    @Published var imageData: Data?
    
    var id: String?
    var alias: String?
    var name: String?
    var imageUrl: String?
    var isClosed: Bool?
    var url: String?
    var reviewCount: Int?
    var categories: [Category]?
    var rating: Double?
    var coordinates: Coordinate?
    var transactions: [String]?
    var price: String?
    var location: Location?
    var phone: String?
    var displayPhone: String?
    var distance: Double?
    
    enum CodeKeys: String, CodingKey {
         
        case imageUrl = "image_url"
        case isClosed = "is_closed"
        case reviewCount = "review_count"
        case displayPhone = "display_phone"
        
        case id
        case alias
        case name
        case url
        case categories
        case rating
        case coordinates
        case transactions
        case price
        case location
        case phone
        case distance
    }
    
    func getImagedata() {
        
        guard imageUrl != nil else {
            return
        }
        
        if let url = URL(string: imageUrl!) {
        
            let session = URLSession.shared
            
            let dataTask = session.dataTask(with: url){(data, response, error) in
                
                if error == nil {
                    
                    DispatchQueue.main.async {
                        
                        self.imageData = data!
                        
                    }
                }
            }
            dataTask.resume()
        }
    }
}

struct Location: Decodable {
    
    var address1: String?
    var address2: String?
    var address3: String?
    var city: String?
    var zip_code: String?
    var country: String?
    var state: String?
    var display_address: [String]?
    
    
    enum codingkey: String, CodingKey{
        
        case zipCode = "zip_code"
        case displayAddress = "display_address"
        case address1
        case address2
        case address3
        case city
        case country
        case state
        
    }
    
    
}

struct Category: Decodable {
    var alias: String?
    var title: String?
}

struct Coordinate: Decodable {
    var latitude: Double?
    var longitude: Double?
}

// SECOND  ATTEMP

    
import Foundation

class Business: Identifiable, ObservableObject , Decodable {
    
   
    
    @Published var imageData: Data?
    
    var id: String?
    var alias: String?
    var name: String?
    var image_url: String?
    var isClosed: Bool?
    var url: String?
    var review_count: Int?
    var categories: [Category]?
    var rating: Double?
    var coordinates: Coordinate?
    var transactions: [String]?
    var price: String?
    var location: Location?
    var phone: String?
    var display_phone: String?
    var distance: Double?
    
    enum CodeKeys: String, CodingKey {
         
        case imageUrl = "image_url"
        case isClosed = "is_closed"
        case reviewCount = "review_count"
        case displayPhone = "display_phone"
        
        case id
        case alias
        case name
        case url
        case categories
        case rating
        case coordinates
        case transactions
        case price
        case location
        case phone
        case distance
    }
    
    func getImagedata() {
        
        guard image_url != nil else {
            return
        }
        
        if let url = URL(string: image_url!) {
        
            let session = URLSession.shared
            
            let dataTask = session.dataTask(with: url){(data, response, error) in
                
                if error == nil {
                    
                    DispatchQueue.main.async {
                        
                        self.imageData = data!
                        
                    }
                }
            }
            dataTask.resume()
        }
    }
}

struct Location: Decodable {
    
    var address1: String?
    var address2: String?
    var address3: String?
    var city: String?
    var zip_code: String?
    var country: String?
    var state: String?
    var display_address: [String]?
    
    
    enum codingkey: String, CodingKey{
        
        case zipCode = "zip_code"
        case displayAddress = "display_address"
        case address1
        case address2
        case address3
        case city
        case country
        case state
        
    }
    
    
}

struct Category: Decodable {
    var alias: String?
    var title: String?
}

struct Coordinate: Decodable {
    var latitude: Double?
    var longitude: Double?
}


Yeah, this is the same issue with me. I have googled it still I could not figure it out. I am waiting for this too. When the answer comes from Christ, just tag me. Thank you.

How i can fix this problems ??

It is almost impossible for us to say unless you show us some code.

And when you post code, make sure you do it as text rather than a screenshot. Place three backticks (```) on the line before your code and three backticks (```) on the line after your code to ensure it gets formatted as a code block. It should look like this:

    ```
    your code goes here
    ```

It’s the code that Chris used in the in the module 6 lesson 7. I have the same problem. It occurs when I entered the code enum CodingKeys inside the class Business

Here’s the code, just in case

mport Foundation
class Business: Decodable, Identifiable, ObservableObject {
    
    
     var imageData: Data?
    
    var id: String?
    var alias: String?
    var name: String?
    var image_url: String?
    var is_closed: Bool?
    var url: String?
    var review_count: Int?
    var categories:[Category]?
    var rating: Double?
    var coordinates: Coordinate?
    var transactions:[String]?
    var price: String?
    var location:Location?
    var phone: String?
    var display_phone: String?
    var distance: Double?
    
//    enum CodingKeys: String, CodingKey {
//
//        case imageURL = "image_url"
//        case isClosed = "is_closed"
//        case reviewCount = "review_count"
//        case displyPhone = "display_phone"
//
//        case id
//        case alias
//        case name
//        case url
//        case categories
//        case rating
//        case coordinates
//        case transactions
//        case price
//        case location
//        case phone
//        case distance
//
//    }
//
//
//    func getImageData() {
//
//        // Check that the image url isn't nil
//        guard image_url != nil else {
//            return
//        }
//        // Download the data for the image
//        if let url = URL(string: image_url!) {
//
//            let session = URLSession.shared
//            let dataTask = session.dataTask(with: url) { data, response, error in
//                if error == nil {
//                    // Set the image data
//                    self.imageData = data!
//
//                }
//            }
//            dataTask.resume()
//        }
//    }
}

struct Location: Decodable {
    var address1: String?
    var address2: String?
    var address3: String?
    var city: String?
    var zipCode: String?
    var country: String?
    var state: String?
    var displayAddress: [String]?
    
    enum CodingKeys: String, CodingKey {
        
        case zipCode = "zip_code"
        case displayAddress = "display_address"
        case address1
        case address2
        case address3
        case city
        case country
        case state
        
    }
}

struct Category: Decodable {
    var alias: String?
    var title: String?
    
}

struct Coordinate: Decodable {
    var latitude: Double?
    var longitude: Double?
}



I appreciate your help

See above. :point_up_2:

I wrote all of Class code .

See if this helps. There were a number of errors and they have been noted.

class Business: Decodable, Identifiable, ObservableObject {
    @Published var imageData: Data?

    var id: String?
    var alias: String?
    var name: String?
    var imageUrl: String?  //  naming error. Was image_url
    var isClosed: Bool?     //  naming error. Was is_closed
    var url: String?
    var reviewCount: Int?  //  naming error. Was review_count
    var categories:[Category]?
    var rating: Double?
    var coordinates: Coordinate?
    var transactions:[String]?
    var price: String?
    var location:Location?
    var phone: String?
    var displayPhone: String? //  naming error. Was display_phone
    var distance: Double?

    enum CodingKeys: String, CodingKey {

        case imageURL = "image_url"
        case isClosed = "is_closed"
        case reviewCount = "review_count"
        case displayPhone = "display_phone"  // spelling error. Was displyPhone

        case id
        case alias
        case name
        case url
        case categories
        case rating
        case coordinates
        case transactions
        case price
        case location
        case phone
        case distance

    }


    func getImageData() {

        // Check that the image url isn't nil
        guard imageUrl != nil else {
            return
        }
        // Download the data for the image
        if let url = URL(string: imageUrl!) {

            let session = URLSession.shared
            let dataTask = session.dataTask(with: url) { data, response, error in
                if error == nil {
                    // Set the image data
                    self.imageData = data!

                }
            }
            dataTask.resume()
        }
    }
}

struct Location: Decodable {
    var address1: String?
    var address2: String?
    var address3: String?
    var city: String?
    var zipCode: String?
    var country: String?
    var state: String?
    var displayAddress: [String]?

    enum CodingKeys: String, CodingKey {

        case zipCode = "zip_code"
        case displayAddress = "display_address"
        case address1
        case address2
        case address3
        case city
        case country
        case state

    }
}

struct Category: Decodable {
    var alias: String?
    var title: String?

}

struct Coordinate: Decodable {
    var latitude: Double?
    var longitude: Double?
}

Fixed!

The problem was that I misspelled some variables and the var categories was missing. What I did was copy the code from the CWC resources folder and it worked. Then I realized of all my errors.

Thanks for your help

but mine is not working yet . I tryed everything .

I downloaded and used a single business class, and also the overall city sight project. however, the same error is noted …

Or is my problem depend on with xcode 13.1 .

Chris __ if you see this post , Please help to us .
To solve this problem .

due to this problem I still cannot complete the city sight project !!!

@Ibrahim9999

Can you compress and upload your project to either DropBox or Google Drive. Create a share link after uploading and post that link in a reply.

here is that my project

Hi.
did you not see my issue yet ?

When you uploaded it to Google Drive and selected the option to share the file, did you leave it as Restricted access or did you set it to “Anyone with the Link”. If you set it to restricted then anyone who wants access has to submit an email address which you add before access is granted. That is too restrictive and I don’t want to mess around providing my email address. Change the share to “anyone with the Link” and let me know when you have done that.

you can log in, I adjusted the settings. for anyone

There are missing files for the two folders shown in red.

There are also significant errors in your Business.swift file.