SwiftUI JSON, ScrollView HStack'te başlığı yazdırmaz (ancak Listede olacaktır)

Aug 17 2020

ContentView.swift dosyamda hatalar alıyorum ve öğrenmeye çalışırken hataların ne dediğini anlamıyorum.

Program türünün değerini Gönderi türüne atayamadığını anlıyorum, ancak başlığı nasıl almam gerekiyor?

Bunu HStack'te yazdırmak için nasıl alabilirim?

import SwiftUI
import RemoteImage

struct ContentView: View {
  
    @State var posts: [Program] = []
    
   var body: some View {
    List(posts){ post in
        
        Text("hello")
        
        /*RemoteImage(type: .url(URL(string:post.url)!), errorView: { error in
            Text(error.localizedDescription)
        }, imageView: { image in
            image
            .resizable()
            .aspectRatio(contentMode: .fit)
        }, loadingView: {
            Text("Loading ...")
        })*/
        
        
       // UrlImageView(post.url)
        } .onAppear {
           Api().getPosts { (posts) in
                            self.posts = posts
                           }
                    }
    

    
   
     
        }
}
import SwiftUI

// Add this top level struct to
// decode properly
struct Post: Codable {
    var programs: [Program]
}

struct Program: Codable, Identifiable {
    let id = UUID()
    var title : String
    var icon : String
}

class Api {
    // Update this to return an array of Program
    func getPosts(completion: @escaping ([Program]) -> ()) {
        guard let url = URL(string: "https://api.drn1.com.au/api-access/programs/DRN1") else { return }
        
        URLSession.shared.dataTask(with: url) { (data, _, _) in
            // Based on the updated structs, you would no
            // longer be decoding an array
            let post = try! JSONDecoder().decode(Post.self, from: data!)
            DispatchQueue.main.async{
                // The array is stored under programs now
                completion(post.programs)
            }
            
        }
    .resume()
    }
}

Bu da işe yarıyor ama kaydırılamıyor.

  ForEach(posts){ post in
                  HStack {Text(post.title)}
               }.onAppear{
                   Api().getPosts { (posts) in
                       self.posts = posts
                   }
               }

net olmak gerekirse, elde etmeye çalıştığım sonuç budur.

ScrollView(.horizontal) {
    HStack(spacing: 20) {
        ForEach(0..<10) {
            Text("Item \($0)")
                .foregroundColor(.white)
                .font(.largeTitle)
                .frame(width: 200, height: 200)
                .background(Color.red)
        }
    }
}

Yanıtlar

RajatMishra Aug 18 2020 at 09:31

Lütfen projeyi temizleyin ve inşa edin. Kod iyi çalışıyor gibi görünüyor. Yeni bir proje oluşturdum ve yazdığınız yukarıdaki kodu kopyalayıp yapıştırdım ve düzgün çalışıyor.

DÜZENLENMİŞ - Listeyi yatay olarak göstermek için lütfen aşağıdaki kodu kullanın:

var body: some View {
    NavigationView {
        if posts.isEmpty {
            Text("Loading")
        } else {
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(alignment: .center, spacing: 10) {
                    ForEach(posts) { post in
                        return Text(post.title)
                    }
                }
            }.frame(height: 200)
        }
    }.onAppear {
        Api().getPosts { (posts) in
            self.posts = posts
        }
    }.navigationBarTitle(Text("Home"))
}