ios - SwiftUI 问题 - 调用中的无关参数标签 'perform:' (Xcode 11 Swift 5)

标签 ios swift xcode swift5 xcode11

我正在学习 Swift 并尝试实现 WWDC 2019 Session 204 中演示的“Room”应用程序。在我下面的代码中,其输入与视频中的 Jacob 完全相同,但我遇到了以下错误:

行:

.onDelete(perform: deleteRoom)

错误:

"Extraneous argument label 'perform:' in call"

我自己无法弄清楚...

提前致谢!

struct ContentView: View {
    //var rooms: [Room] = []
    //    @ObservedObject var store = RoomStore()
    var store = RoomStore()

    var body: some View {
        NavigationView {
            List {
                Section {
                    Button(action: addRoom) {
                        Text("Add")
                    }
                }

                Section {
                    ForEach(store.rooms) { room in
                        RoomCell(room: room)
                    }
                    /* HERE is the error */
                    .onDelete(perform: deleteRoom)
                }
            }
            .navigationBarTitle(Text("Rooms"))
            .listStyle(.grouped)
        }
    }

    func addRoom() {
        store.rooms.append(Room(name: "New Room", capacity: 20, hasVideo: true))
    }

    func deleteRoom(at offsets: IndexSet) {
        store.rooms.remove(atOffsets: offsets)
    }
}

最佳答案

Meme

不要信任 Xcode:

Xcode 还不够智能,无法告诉您 SwiftUI 中的真正问题是什么(还)。不管你信不信,问题出在 listStyle 上。

您应该将其更改为:

.listStyle(GroupedListStyle())

不要忘记从您在第一部分中意外输入的 .Text("Add") 中删除 .


一些其他有用的注释(与该问题无关):

SwiftUI API 现在与 String 更加兼容,因此您可以直接在 View 的某些初始化程序中设置 TextButton修饰符navigationBarTitle:

Button("Add", action: addRoom) /* Instead of Button(action: addRoom) { Text("Add") } */
.navigationBarTitle("Rooms") /* Instead of .navigationBarTitle(Text("Rooms")) */

关于ios - SwiftUI 问题 - 调用中的无关参数标签 'perform:' (Xcode 11 Swift 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59037008/

相关文章:

ios - 在 UIKit View 层次结构中感到困惑

ios - 使用自动完成文本字段时不调用 cellForRowAtIndexPath 方法

iphone - writeToFile 失败 - 如何调试它失败的原因(什么工具)?

c++ - 使用#define 和 clang 为 info.plist 创建版本号变化?

ios - iOS 上的 crt_externs.h ?

ios - NSDateFormatter dateFromString 返回错误的日期

ios - 如何在 iPhone 中管理多任务

来自 Amazon S3 MP4/MOV URL 的 iOS 缩略图

swift - 尝试在 Swift 中使用 PartialKeyPath 修改类属性

ios - 图像上类似 Snapchat 的文字