天道酬勤,学无止境

swift-class

如何更新视图中结构的值(How to update the values of a struct in a View)

问题 在 SwiftUI 中,我有一个想要保存视图数据的结构。 假设有一个用户可以在其中创建配方的视图。 它有一个用于输入配方名称的文本字段,以及用于选择和添加到结构属性中的数组的选项。 我设法制作了结构并将其引入到视图中,但我无法更改它的值。 结构的值应该根据用户在视图上所做的事情以及他们添加到其中的信息进行更新。 我做了一个简单的视图,在 TextField 中输入的任何内容都将添加到结构的属性之一中。 对于下面的代码,我得到了无法分配给属性:'self' 是不可变的 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 } 看法:

2021-10-27 02:37:32    分类:技术分享    swift   struct   swiftui   swift-structs   swift-class

How to update the values of a struct in a View

In SwiftUI I have a struct that want to hold data of the View. Let's say there is a View that user can create a recipe in it. It has a text field to type the recipe name, and options to choose and add to the array in the struct's properties. I managed to make the struct and introduce it in the View but I cannot change it values. The values of the struct should be updated based on what users does on the View and the info they add to it. I made a simple View that whatever is entered in the TextField will be added in one of the properties of the struct. For the below code I get Cannot assign to

2021-10-25 12:17:13    分类:问答    swift   struct   swiftui   swift-structs   swift-class