as soon as I add the enum for codingKeys it start producing weird problem.
Change your
var display_address: [String]?
to
var displayAddress: [String]?
and change your
var zip_code: String?
to
var zipCode: String?
Also, 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. 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.
thank and will keep in mind.
also I’ve done than after creating enum.
when I remove the enum then it works fine
import SwiftUI
struct Business: Decodable, Identifiable {
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 category:[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 displayPhone = "display_phone"
case id
case alias
case name
case url
case category
case rating
case coordinates
case transactions
case price
case location
case phone
case distance
}
}
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?
}
In your structs the enum case name
must match the var name
that you declare. It is absolutely essential that the spelling is identical otherwise it will not work.
This is your version of the structs corrected. See if that works for you.
import Foundation
struct Business: Decodable, Identifiable {
var id: String?
var alias: String?
var name: String?
var imageUrl: String?
var isClosed: Bool?
var url: String?
var reviewCount: Int?
var category:[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 codingKeys: 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 category
case rating
case coordinates
case transactions
case price
case location
case phone
case distance
}
}
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?
}
What the enums are doing is translating the name of the properties that are provided in the JSON file to the properties you are using in your code.
The preference for swift variable naming is what is known as “camel case” which is where you have the first letter as lower case and the other words in the variable name have their first letter upper case. The API naming convention is often “snake case”
For example where the API provided display_name
(“snake case”) the Swift version of that is displayName
(“camel case”).
Does that make sense?
Hi Bharat,
When you created your project it looks like you created it as a multi-platform App rather than creating it as an iOS App. ie, when you create an iOS App you should select the options as per the following screenshot.
I doubt that it is having an impact on your App but it means that it looks a lot different than the App should when you are following along in a tutorial. It’s possible that you may get a little confused comparing what you are looking at to what you are seeing in the tutorial described by Chris Ching. That is especially the case if you are relatively new to Xcode.
When I opened your project there were two folders that showed up in red. They are named OnBoading and DeniedView. There were no swift files in either of those folders.
Unfortunately the project appears to be corrupted and would not build saying that ContentModel and BusinessList were not in scope despite the two files being in the App Bundle. What I did was re-create it as an iOS App and copy across the code to that new project. It runs and I see the same issue you describe in that Restaurants and Sights show the same data. I am not sure why that is the case. Still doing some testing.
Thank you so much Chris . I was able to figure out the problem so I deleted the reply. For some reason Proxyman is not working for me the Xcode is throwing errors. But other then that I’m good. Thanks a lot!! you are a saviour
What was the problem?