如何更新视图中结构的值
How to update the values of a struct in a View
在 SwiftUI 中,我有一个要保存视图数据的结构。假设有一个视图,用户可以在其中创建食谱。它有一个用于输入配方名称的文本字段,以及用于选择和添加到结构属性中的数组的选项。
我设法创建了结构并将其引入视图中,但我无法更改它的值。结构的值应该根据用户在视图上的操作以及他们添加到视图中的信息进行更新。
我制作了一个简单的视图,即在 TextField 中输入的任何内容都将添加到该结构的一个属性中。
对于下面的代码,我得到 Cannot assign to 属性: 'self' is immutable
struct Recipe: Codable {
let user: Int
var recipeName, createdDate: String
let ingredients: [Ingredient]
let equipment: [[Int]]
enum CodingKeys: String, CodingKey {
case user = "User"
case recipeName = "RecipeName"
case createdDate
case ingredients = "Ingredients"
case equipment = "Equipment"
}
}
struct Ingredient: Codable {
let ingredient, size: Int
}
查看:
struct ContentView: View {
var recipe = Recipe(user: 1231, recipeName: "Recipe Name", createdDate: "SomeDate", ingredients: [Ingredient(ingredient: 1, size: 2)], equipment: [[1],[4],[5]])
var body: some View {
VStack {
Text(self.recipe.recipeName)
Button(action: {
recipe.recipeName = "New Name" //Cannot assign to property: 'self' is immutable
}) {
Text("Change Name")
}
}
}
}
知道如何解决这个问题,以便我可以与结构交互并更新其属性。我也会在其他视图中使用这个结构变量。
提前致谢
添加@State
@State var recipe = Recipe(..
此外,不要忘记在使用 recipe
内部按钮操作之前添加 self.
。
对于所提供的用例,最合适的是使用视图模型,如下例所示
class RecipeViewModel: ObservableObject {
@Published var recipe: Recipe
init(_ recipe: Recipe) {
self.recipe = recipe
}
}
所以在视图中
struct ContentView: View {
@ObservedObject var recipeVM = RecipeViewModel(Recipe(user: 1231, recipeName: "Recipe Name", createdDate: "SomeDate", ingredients: [Ingredient(ingredient: 1, size: 2)], equipment: [[1],[4],[5]]))
var body: some View {
VStack {
Text(self.recipeVM.recipe.recipeName)
Button(action: {
self.recipeVM.recipe.recipeName = "New Name"
}) {
Text("Change Name")
}
}
}
}
在 SwiftUI 中,我有一个要保存视图数据的结构。假设有一个视图,用户可以在其中创建食谱。它有一个用于输入配方名称的文本字段,以及用于选择和添加到结构属性中的数组的选项。
我设法创建了结构并将其引入视图中,但我无法更改它的值。结构的值应该根据用户在视图上的操作以及他们添加到视图中的信息进行更新。 我制作了一个简单的视图,即在 TextField 中输入的任何内容都将添加到该结构的一个属性中。 对于下面的代码,我得到 Cannot assign to 属性: 'self' is immutable
struct Recipe: Codable {
let user: Int
var recipeName, createdDate: String
let ingredients: [Ingredient]
let equipment: [[Int]]
enum CodingKeys: String, CodingKey {
case user = "User"
case recipeName = "RecipeName"
case createdDate
case ingredients = "Ingredients"
case equipment = "Equipment"
}
}
struct Ingredient: Codable {
let ingredient, size: Int
}
查看:
struct ContentView: View {
var recipe = Recipe(user: 1231, recipeName: "Recipe Name", createdDate: "SomeDate", ingredients: [Ingredient(ingredient: 1, size: 2)], equipment: [[1],[4],[5]])
var body: some View {
VStack {
Text(self.recipe.recipeName)
Button(action: {
recipe.recipeName = "New Name" //Cannot assign to property: 'self' is immutable
}) {
Text("Change Name")
}
}
}
}
知道如何解决这个问题,以便我可以与结构交互并更新其属性。我也会在其他视图中使用这个结构变量。
提前致谢
添加@State
@State var recipe = Recipe(..
此外,不要忘记在使用 recipe
内部按钮操作之前添加 self.
。
对于所提供的用例,最合适的是使用视图模型,如下例所示
class RecipeViewModel: ObservableObject {
@Published var recipe: Recipe
init(_ recipe: Recipe) {
self.recipe = recipe
}
}
所以在视图中
struct ContentView: View {
@ObservedObject var recipeVM = RecipeViewModel(Recipe(user: 1231, recipeName: "Recipe Name", createdDate: "SomeDate", ingredients: [Ingredient(ingredient: 1, size: 2)], equipment: [[1],[4],[5]]))
var body: some View {
VStack {
Text(self.recipeVM.recipe.recipeName)
Button(action: {
self.recipeVM.recipe.recipeName = "New Name"
}) {
Text("Change Name")
}
}
}
}