因此,我试图用JSON填充数据列表(在这种情况下,它们是书),JSON包含每本书的图像,我问了一下,而Image Loader应该是唯一的方法做吧。
所以让我给你看一些代码。
这是图像加载器:
struct ImageView: View {
@ObservedObject var imageLoader:ImageLoader
@State var image:UIImage = UIImage()
func imageFromData(_ data:Data) -> UIImage {
UIImage(data: data) ?? UIImage()
}
init(withURL url:String) {
imageLoader = ImageLoader(urlString:url)
}
var body: some View {
VStack {
Image(uiImage: imageLoader.data != nil ? UIImage(data:imageLoader.data!)! : UIImage())
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width:100, height:100)
}
}
}
class ImageLoader: ObservableObject {
@Published var dataIsValid = false
var data:Data?
init(urlString:String) {
guard let url = URL(string: urlString) else { return }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
DispatchQueue.main.async {
self.dataIsValid = true
self.data = data
}
}
task.resume()
}
}
看看VStack
的{{1}}中body: some View
的第一行,它崩溃了,给了我struct
。
现在,这是Unexpectedly found nil while unwrapping an Optional value
的代码:
List
let apiUrl = "https://qodyhvpf8b.execute-api.us-east-1.amazonaws.com/test/books"
class BooksViewModel: ObservableObject {
@Published var books: [Book] = [
.init(id: 1, nombre: "Libro 1", autor: "Autor 1", disponibilidad: true, popularidad: 100, imagen: "https://www.google.com.ar"),
.init(id: 2, nombre: "Libro 2", autor: "Autor 2", disponibilidad: false, popularidad: 80, imagen: "https://www.google.com.ar"),
.init(id: 3, nombre: "Libro 3", autor: "Autor 3", disponibilidad: true, popularidad: 60, imagen: "https://www.google.com.ar")
]
func fetchBooks() {
guard let url = URL(string: apiUrl) else { return }
URLSession.shared.dataTask(with: url) { (data, resp, err) in
DispatchQueue.main.async {
do {
self.books = try JSONDecoder().decode([Book].self, from: data!)
} catch {
print("Failed to decode JSON: ", error)
}
}
}.resume()
}
}
struct ContentView: View {
@ObservedObject var booksVM = BooksViewModel()
var body: some View {
NavigationView {
List {
VStack(alignment: .leading) {
ForEach(booksVM.books.sorted { $0.popularidad > $1.popularidad}) { book in
HStack {
ImageView(withURL: book.imagen)
Text(book.nombre)
Spacer()
Text(String(book.popularidad))
}
HStack {
Text(book.autor)
Spacer()
if book.disponibilidad {
Text("Disponible")
} else {
Text("No disponible")
}
}
Spacer()
}
}.padding([.leading, .trailing], 5)
}
.navigationBarTitle("Bienvenido a la librería flux")
.onAppear(perform: self.booksVM.fetchBooks)
}
}
}
,HStack
的第一行是应该传递URL字符串的行,但是我不知道为什么,这种情况没有发生。 ImageView(withURL: book.imagen)
应该返回一个字符串。我不知道是否是因为它没有返回字符串,或者图像加载器上出现了问题。无法解决。其余数据就很好了。
这是JSON的一部分:
book.imagen
If it helps, this is what I'm trying to achieve
请让我知道是否需要进一步说明。
答案 0 :(得分:0)
swiftUI代码没有错。这是有效的模型样本代码。也许您错过了模型中的某些东西。
struct Book: Identifiable, Decodable {
var popularidad: Int = 0
var imagen : String = ""
var nombre : String = ""
var autor : String = ""
var disponibilidad : Bool = false
var id : Int = 0
}
class BooksViewModel: ObservableObject {
@Published var books : [Book] = []
func fetchBooks(){
do{
if let url = Bundle.main.url(forResource: "LocalCache", withExtension: "json"){
if let string = try String(contentsOf: url).data(using: .utf8){
self.books = try JSONDecoder().decode([Book].self, from: string)}
}
}
catch{}
}
}