After I did update Xcode to 14.3.1 any File.swift that I add can't be found in the scope
can someone point me to the solution please??
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Im trying to decode this JSON with SwiftUI but I have this error below there is my code:
JSON
{
"page": 2,
"per_page": 6,
"total": 12,
"total_pages": 2,
"data": [
{
"id": 7,
"email": "michael.lawson@reqres.in",
"first_name": "Michael",
"last_name": "Lawson",
"avatar": "https://reqres.in/img/faces/7-image.jpg"
},
{
"id": 8,
"email": "lindsay.ferguson@reqres.in",
"first_name": "Lindsay",
"last_name": "Ferguson",
"avatar": "https://reqres.in/img/faces/8-image.jpg"
},
{
"id": 9,
"email": "tobias.funke@reqres.in",
"first_name": "Tobias",
"last_name": "Funke",
"avatar": "https://reqres.in/img/faces/9-image.jpg"
},
{
"id": 10,
"email": "byron.fields@reqres.in",
"first_name": "Byron",
"last_name": "Fields",
"avatar": "https://reqres.in/img/faces/10-image.jpg"
},
{
"id": 11,
"email": "george.edwards@reqres.in",
"first_name": "George",
"last_name": "Edwards",
"avatar": "https://reqres.in/img/faces/11-image.jpg"
},
{
"id": 12,
"email": "rachel.howell@reqres.in",
"first_name": "Rachel",
"last_name": "Howell",
"avatar": "https://reqres.in/img/faces/12-image.jpg"
}
],
"support": {
"url": "https://reqres.in/#support-heading",
"text": "To keep ReqRes free, contributions towards server costs are appreciated!"
}
}
DataManager
// MARK: - Menu
struct Menu: Codable {
let page, perPage, total, totalPages: Int
let data: [Datum]
let support: Support
enum CodingKeys: String, CodingKey {
case page
case perPage = "per_page"
case total
case totalPages = "total_pages"
case data, support
}
}
// MARK: - Datum
struct Datum: Codable {
let id: Int
let email, firstName, lastName: String
let avatar: String
enum CodingKeys: String, CodingKey {
case id, email
case firstName = "first_name"
case lastName = "last_name"
case avatar
}
}
// MARK: - Support
struct Support: Codable {
let url: String
let text: String
}
class DataManager: ObservableObject{
@Published var menu: [Menu] = []
func fetch(){
guard let url = URL(string: "https://reqres.in/api/users?page=2") else { return }
// var request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: url) {data, _, error in
guard let data = data, error == nil else { return }
do{
let menu = try JSONDecoder().decode([Menu].self, from: data)
DispatchQueue.main.async {
self.menu = menu
}
}catch{
print(error)
}
}
task.resume()
}
}
ContentView
import SwiftUI
struct ContentView: View {
@StateObject var profiles = DataManager()
var body: some View {
NavigationStack{
List{
ForEach(profiles.profile){ profile in
Text(profile.firstName)
}
}.onAppear{
DataManager().fetch()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Could please anyone help me with that??
After I did update Xcode, I've got this error. The new file was create with the right target and in the right folder but I still got that error. Can anyone point me in the right direction??