方法有问题。无法对不可变值使用变异成员:“book”;是一个“让”持续的

发布于 2025-01-11 14:40:04 字数 2756 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文