作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 SwiftUI 应用程序,它使用公共(public) API 按名称下载鸡尾酒数据,但我对 SwiftUI 不是很熟悉,而且我看不到在我的 DetailsView 文件中初始化我的 View 模型的方法。
如果鸡尾酒数据结构,这是我的 swift 文件;
struct Drinks: Decodable {
var cocktails: [Cocktail]
}
struct Cocktail: Decodable, Identifiable {
var id: String {
return idDrink
}
let idDrink: String
let strDrink: String
let strDrinkThumb: String
let strAlcoholic: String
let strGlass: String
let strInstructions: String
let strIngredient1: String?
let strIngredient2: String?
let strIngredient3: String?
let strIngredient4: String?
let strIngredient5: String?
let strIngredient6: String?
let strIngredient7: String?
let strIngredient8: String?
let strIngredient9: String?
let strIngredient10: String?
let strIngredient11: String?
let strIngredient12: String?
}
这是我的 NetworkManager 类;
class NetworkManager {
func fetchData(_ urlString: String, completion: @escaping (Drinks, Bool) -> Void) {
guard let url = URL(string: urlString) else { return }
var drinks: Drinks?
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { data, response, error in
if error == nil {
let decoder = JSONDecoder()
guard let safeData = data else { return }
do {
drinks = try decoder.decode(Drinks.self, from: safeData)
completion(drinks!, false)
} catch let error {
print(error.localizedDescription)
if error.localizedDescription == "The data couldn’t be read because it is missing." {
completion(drinks ?? Drinks(cocktails: [Cocktail]()), true)
} else {
print(error.localizedDescription)
}
}
}
}
task.resume()
}
}
这是我的 ViewModel 类;
class ViewModel: ObservableObject {
let networkManager = NetworkManager()
var urlString: String
@Published var drinks: Drinks = Drinks(cocktails: [Cocktail]())
@Published var dataIsFound: Bool = true
init(urlString: String) {
self.urlString = urlString
FetchData()
}
func FetchData() {
networkManager.fetchData(urlString) { results, error in
DispatchQueue.main.async {
self.drinks = results
self.dataIsFound = !error
}
}
}
这是我的 DetailsView 结构;
struct DetailsView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
List(viewModel.drinks.cocktails) { cocktail in
VStack(alignment: .center) {
HStack(alignment: .center) {
Text(cocktail.strDrink + " -")
.navigationTitle("Cocktail by first letter")
.frame(alignment: .center)
Text(cocktail.strAlcoholic)
.frame(alignment: .center)
}
WebImage(url: URL(string: cocktail.strDrinkThumb))
.resizable()
.frame(width: UIScreen.main.bounds.width - 20.0, height: UIScreen.main.bounds.width - 20.0, alignment: .center)
Text("~ Ingredients List ~\n").frame(alignment: .center)
ForEach(viewModel.buildIngredients(cocktail), id: \.self) { ingredient in
Text(ingredient)
}
Text("\n~ Recipe Instructions ~\n\n")
Text(cocktail.strInstructions + "\n").fixedSize(horizontal: false, vertical: true)
}
}
}
}
如有任何帮助,我们将不胜感激。谢谢。
最佳答案
这是一种常见的模式,使用 ViewModel()
进行初始化,然后在 onAppear
上调用 fetchData
:
class ViewModel: ObservableObject {
let networkManager = NetworkManager()
@Published var drinks: Drinks = Drinks(cocktails: [Cocktail]())
@Published var dataIsFound: Bool = true
func fetchData(urlString: String) {
//call fetchData on network manager
}
}
struct DetailsView: View {
var urlString : String
@StateObject private var viewModel = ViewModel()
var body: some View {
List(viewModel.drinks.cocktails) { cocktail in
//list content
}
.onAppear {
viewModel.fetchData(urlString: urlString)
}
}
}
另一种选择是使用您的 View
的init
。在这种情况下,@StateObject
的 init 将通过传递到 View
的 urlString
调用。因为 StateObject
的 wrappedValue
参数使用自动关闭并且只有在将 View 添加到层次结构时才会运行,所以您不必担心 View 模型会被重新- 在 View
的每个 init
上初始化。
class ViewModel: ObservableObject {
let networkManager = NetworkManager()
@Published var drinks: Drinks = Drinks(cocktails: [Cocktail]())
@Published var dataIsFound: Bool = true
init(urlString: String) {
fetchData(urlString: urlString)
}
func fetchData(urlString: String) {
//call fetchData on network manager
}
}
struct DetailsView: View {
@StateObject private var viewModel : ViewModel
init(urlString: String) {
_viewModel = StateObject(wrappedValue: ViewModel(urlString: urlString))
}
var body: some View {
//body content
}
}
关于ios - 如何在我的 SwiftUI 应用程序中初始化我的 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69725312/
我是一名优秀的程序员,十分优秀!