Json 使用TextField中的值执行API调用,并在SwiftUI中显示结果
希望使用Textfield中的值执行API调用,并在列表中显示结果。无法将textfield中的$name值用于url,然后进行API调用 到目前为止我的代码 WebService.swiftJson 使用TextField中的值执行API调用,并在SwiftUI中显示结果,json,swiftui,textfield,Json,Swiftui,Textfield,希望使用Textfield中的值执行API调用,并在列表中显示结果。无法将textfield中的$name值用于url,然后进行API调用 到目前为止我的代码 WebService.swift import Foundation public class UserFetcher: ObservableObject { @Published var shoes: [Shoe] = [] @Published private var name: String = "&q
import Foundation
public class UserFetcher: ObservableObject {
@Published var shoes: [Shoe] = []
@Published private var name: String = ""
init(){
load()
}
func load() {
// let url = URL(string: "https://api.thesneakerdatabase.com/v1/sneakers?limit=10&name=wolf%20in%20sheeps%20clothing")! // the format the API requires
let url = URL(string: "https://api.thesneakerdatabase.com/v1/sneakers?limit=10&name=" + name)!
URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
if let d = data {
let decodedLists = try JSONDecoder().decode(APIResponse.self, from: d)
DispatchQueue.main.async {
// self.users = decodedLists
self.shoes = decodedLists.shoeResults
}
}else {
print("No Data")
}
} catch {
print ("Error")
}
}.resume()
}
}
ContentView.swift
import Foundation
import SwiftUI
import Combine
import SDWebImage
import SDWebImageSwiftUI
struct ContentView: View {
@ObservedObject var fetcher = UserFetcher()
@State private var name: String = ""
var body: some View {
NavigationView {
VStack {
VStack(alignment: .leading) {
TextField("Search", text: $name)
}
List(fetcher.shoes) { Shoe in
VStack (alignment: .leading) {
Text(Shoe.name)
Text(Shoe.shoe)
.font(.system(size: 11))
.foregroundColor(Color.gray)
Text(Shoe.brand)
.font(.system(size: 11))
.foregroundColor(Color.gray)
Text(Shoe.styleId)
.font(.system(size: 11))
.foregroundColor(Color.gray)
Text(".\(Shoe.year)")
.font(.system(size: 11))
.foregroundColor(Color.gray)
WebImage(url: URL(string: "\(Shoe.media.thumbUrl)"))
.resizable()
.aspectRatio(contentMode: .fill)
}
}
}
.navigationBarTitle(Text("Search"))
}
}
我相信这是很明显的事情,但在过去的两个小时里我一直盯着它看,似乎无法工作。如果您有任何指示,我们将不胜感激。谢谢您请尝试以下内容:
public class UserFetcher: ObservableObject {
@Published var shoes: [Shoe] = []
init(){
//load() <- remove this
}
func load(_ name: String) { //<-- add parameter
....
}
public类UserFetcher:observeObject{
@发布的鞋:[鞋]=[]
init(){
//load()如果您想要在文本字段中键入并查看结果更新的正常行为,您可能需要类似的内容(请参阅有关更改内容的内联注释):
我还注意到,在测试时,您使用您之前提出的问题中的Shoe
结构从您的API中得到错误——请确保retailPrice
是Int?
而不是Int
,因为有时API不会为该字段返回值
我还将注意到,您可以通过使用Future
将加载
问题转化为可以链接到联合调用的内容,从而使这一点更加流畅,但这是值得进行其他研究或提出不同问题的内容(此处不适用).您好,谢谢您的时间,尝试了一下,似乎出现了以下错误:无法在onAppear上调用非函数类型“UserFetcher”的值,我想我需要添加perform:}.onAppear(perform:{fetcher(name)})
onAppear(){fetcher.load(name)}
struct ContentView: View {
@ObservedObject var fetcher = UserFetcher()
@State private var name: String = ""
var body: some View {
NavigationView {
VStack {
VStack(alignment: .leading) {
TextField("Search", text: $name)
}
List(fetcher.shoes) { Shoe in
...
}
}.onAppear(fetcher.load(name)) //<--- begin to load
}
}
import SwiftUI
import Combine
public class UserFetcher: ObservableObject {
@Published var shoes: [Shoe] = []
@Published var name: String = "" // Not private any more
private var cancellable : AnyCancellable?
init() {
cancellable = $name
.debounce(for: .seconds(1), scheduler: RunLoop.main) //only run once the text has been static for 1 second
.sink { (newSearch) in
self.load(searchTerm: newSearch)
}
}
func load(searchTerm: String) { //take the search term as a parameter
guard !searchTerm.isEmpty, let url = URL(string: "https://api.thesneakerdatabase.com/v1/sneakers?limit=10&name=\(searchTerm)") else {
return //don't search if the term is empty or the URL is invalid
}
URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
if let d = data {
let decodedLists = try JSONDecoder().decode(APIResponse.self, from: d)
DispatchQueue.main.async {
self.shoes = decodedLists.shoeResults
}
}else {
print("No Data")
}
} catch {
print ("Error: \(error)") //print your error here
}
}.resume()
}
}
struct ContentView: View {
@ObservedObject var fetcher = UserFetcher()
var body: some View {
NavigationView {
VStack {
VStack(alignment: .leading) {
TextField("Search", text: $fetcher.name) //using the name property from fetcher now
}
List(fetcher.shoes) { shoe in //don't use capital letters for variable names -- not idiomatically correct Swift and it can get confused with your `Shoe` type
VStack (alignment: .leading) {
Text(shoe.name)
Text(shoe.shoe)
.font(.system(size: 11))
.foregroundColor(Color.gray)
Text(shoe.brand)
.font(.system(size: 11))
.foregroundColor(Color.gray)
Text(shoe.styleId)
.font(.system(size: 11))
.foregroundColor(Color.gray)
Text(".\(shoe.year)")
.font(.system(size: 11))
.foregroundColor(Color.gray)
}
}
}
.navigationBarTitle(Text("Search"))
}
}
}