方法有问题。无法对不可变值使用变异成员:“book”;是一个“让”持续的
我对 SwiftUI 很陌生(学习了几天),在学习基础知识时,我遇到了一个我似乎无法弄清楚的问题。
我想要一个方法(changeBookRead()),该方法将在单击按钮时切换书籍变量“读取”。
我认为问题是,这本书是数组内部的结构实例,但我不确定这是否正确。
如果您能帮助我解决问题,我将不胜感激。
带有changeBookRead()方法的Book结构
struct ContentView: View {
@State private var title = String()
@State private var author = String()
@State private var isRead = false;
@State private var books = [Book]()
@State private var showingAlert = false
func toggleRead () {
isRead.toggle()
}
struct Book {
let ID = UUID()
let bookTitle: String
let bookAuthor: String
@State var read: Bool
// Function that changes read status of specific book instance
mutating func changeBookRead () {
read.toggle()
print(read)
}
}
func addToBooks () {
let book = Book(bookTitle: title, bookAuthor: author, read: isRead)
books.append(book)
}
主体部分 - ForEach循环中的最后一个按钮是我想要使用的按钮。
var body: some View {
NavigationView{
VStack{
TextField("Book title", text: $title)
.padding()
TextField("Book author", text: $author)
.padding()
Group{
Toggle(isOn: $isRead) {
Text("Did I read it...?")
}
.toggleStyle(SwitchToggleStyle(tint: .orange))
.padding()
.frame(minWidth: 100)
.animation(.default, value:isRead)
Button("Add book") {
addToBooks()
}
.buttonStyle(.bordered)
.tint(.orange)
}
.navigationTitle("My Books")
ForEach(books, id: \.ID) { book in
VStack {
Text("Book Title: \(book.bookTitle)")
Text("Book Author: \(book.bookAuthor)")
Button("Remove book", role: .destructive) {
showingAlert.toggle()
}
.alert("Do you want to remove this book?", isPresented: $showingAlert) {
Button("Remove", role: .destructive) {
books.removeAll(where: { $0.ID == book.ID})
}
}
.buttonStyle(.bordered)
Button(book.read ? "Is Read" : "Want to read") {
book.changeBookRead()
}
Divider()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
I am very new to SwiftUI (few days of studying), and when learning basics, I've encountered a problem that I can't seem to figure out.
I would like to have a method (changeBookRead()) that will toggle book variable "read" when button is clicked.
The problem is, I think, that the book is structs instance inside of an array, but I am not sure if that's correct.
I will appreciate if you can help me with solution.
Book struct with changeBookRead() method
struct ContentView: View {
@State private var title = String()
@State private var author = String()
@State private var isRead = false;
@State private var books = [Book]()
@State private var showingAlert = false
func toggleRead () {
isRead.toggle()
}
struct Book {
let ID = UUID()
let bookTitle: String
let bookAuthor: String
@State var read: Bool
// Function that changes read status of specific book instance
mutating func changeBookRead () {
read.toggle()
print(read)
}
}
func addToBooks () {
let book = Book(bookTitle: title, bookAuthor: author, read: isRead)
books.append(book)
}
Body section - last button in the ForEach loop is the one I want to use.
var body: some View {
NavigationView{
VStack{
TextField("Book title", text: $title)
.padding()
TextField("Book author", text: $author)
.padding()
Group{
Toggle(isOn: $isRead) {
Text("Did I read it...?")
}
.toggleStyle(SwitchToggleStyle(tint: .orange))
.padding()
.frame(minWidth: 100)
.animation(.default, value:isRead)
Button("Add book") {
addToBooks()
}
.buttonStyle(.bordered)
.tint(.orange)
}
.navigationTitle("My Books")
ForEach(books, id: \.ID) { book in
VStack {
Text("Book Title: \(book.bookTitle)")
Text("Book Author: \(book.bookAuthor)")
Button("Remove book", role: .destructive) {
showingAlert.toggle()
}
.alert("Do you want to remove this book?", isPresented: $showingAlert) {
Button("Remove", role: .destructive) {
books.removeAll(where: { $0.ID == book.ID})
}
}
.buttonStyle(.bordered)
Button(book.read ? "Is Read" : "Want to read") {
book.changeBookRead()
}
Divider()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论