1

I am new to SwiftUI and only used UIKit before. I tried to use JSON to show a title but all tutorial videos work with lists. I dont want to use any list with JSON which shows all data. Only want to fetch for example the second or a specific array for title.

How can I remove the list in SwiftUI? My View:

struct ContentView: View {
    
    @ObservedObject var networkManager = NetworkManager()
    var body: some View {
            NavigationView {
            List(networkManager.posts) { post in
            HStack {
            Text(String(post.points))
            Text(post.title)
                    }}
            .navigationBarTitle("H4X0R NEWS")
        }
        .onAppear {
            self.networkManager.fetchData()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

NetworkManager:

class NetworkManager: ObservableObject {
    
    @Published var posts = [Post]()
    
    func fetchData() {
        if let url = URL(string: "https://hn.algolia.com/api/v1/search?tags=front_page") {
            let session = URLSession(configuration: .default)
            let task = session.dataTask(with: url) { (data, response, error) in
                if error == nil {
                    let decoder = JSONDecoder()
                    if let safeData = data {
                        do {
                            let results = try decoder.decode(Results.self, from: safeData)
                            DispatchQueue.main.async {
                                self.posts = results.hits
                            }
                        } catch {
                            print(error)
                        }
                    }
                }
            }
            task.resume()
        }
    }
}

And my struct files for Json:

struct Results: Decodable {
    let hits: [Post]
}

struct Post: Decodable, Identifiable {
    var id: String {
        return objectID
    }
    let objectID: String
    let points: Int
    let title: String
}
0

1 Answer 1

1

I dont want to use any list with JSON which shows all data. Only want to fetch for example the second or a specific array for title.

You can use a computed property to access the specific element (and its title) from the posts array:

struct ContentView: View {
    @ObservedObject var networkManager = NetworkManager()

    // return the title of the second item in the `posts` array
    var title: String {
        guard networkManager.posts.count >= 2 else { 
            // decide what to do when data is not yet loaded or count is <= 1
            return "Loading..."
        }
        return networkManager.posts[1].title
    }

    var body: some View {
        NavigationView {
            Text(title)
                .navigationBarTitle("H4X0R NEWS")
        }
        .onAppear {
            self.networkManager.fetchData()
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.