简体   繁体   English

类型 '() -> ()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议

[英]Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

I'm running a function in order to populate a list of expenses.我正在运行一个函数来填充费用列表。 I need to run the function to populate the list using .onAppear(perform: fetchRows) so that the list updates as soon as the the view appears.我需要运行函数来使用.onAppear(perform: fetchRows)填充列表,以便在视图出现时列表更新。

Xcode doesn't like the fact that I'm running a function to populate expensesList. Xcode 不喜欢我正在运行一个函数来填充费用列表的事实。 How can I make this work?我怎样才能使这项工作? Is there a better way of going about this?有没有更好的方法来解决这个问题?

@State var expensesList: [ExpensesList]?

var body: some View {
    
    NavigationView{
        VStack{
                
                if expensesList != nil{
                    
                    
                    List{
                        
                        ForEach(self.expensesList!) {   <-- Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
                            (log: ExpensesList) in {
                                Button(action: {
                                    self.editExpense = log
                                })
                            
                            ExpenseRow(name: log.expense, date: log.date, amount: log.cost, account: log.account)
                            }
                        }.onDelete(perform: onDelete)
                    }
                }
            }.navigationTitle("Expense List")
struct ExpensesList: Identifiable, Equatable {
    let id: UUID
    let expense: String
    let cost: Double
    let account: String
    let date: Date
}
func fetchRows(){
    Expenses.fetchAllExpensesInDateRange(context: self.context) { (results) in
        guard !results.isEmpty else { return }
        
        self.expensesList = results.map({ (result) -> ExpensesList in
            print(result.id,result.expenseName,result.expenseCost, result.expenseAccount, result.expenseDate)
            return ExpensesList(id: result.id, expense: result.expenseName, cost: result.expenseCost, account: result.expenseAccount, date: result.expenseDate)
        })
    }
}

The sheet I use to edit expenses is here.我用来编辑费用的表格在这里。 As you can see I use a Core Data entity called Expenses to store my expense objects.如您所见,我使用名为 Expenses 的 Core Data 实体来存储我的费用对象。

                .sheet(item: $editExpense, onDismiss: {
                    self.editExpense = nil
                })
                {
                    (log: Expenses) in
                    ExpenseDetail(
                        context: self.context,
                        editExpense: log,
                        name: log.expenseName ?? "",
                        amount: String(log.expenseCost),
                        category: log.expenseCategory ?? "",
                        date: log.expenseDate ?? Date(),
                        account: log.expenseAccount ?? "",
                        isMonthly: log.expenseIsMonthly
                    ).environment(\.managedObjectContext, self.context)
                    
                    
                    
                }

You have an extra { after (log: ExpensesList) in { and your Button has no content parameter.您有一个额外的{ after (log: ExpensesList) in {并且您的Button没有content参数。

Try the following instead (I assume that ExpenseRow should be the content of the Button):请尝试以下操作(我假设ExpenseRow应该是 Button 的content ):

ForEach(self.expensesList!) { log in
    Button(action: {
        self.editExpense = log
    }) {
        ExpenseRow(name: log.expense, date: log.date, amount: log.cost, account: log.account)
    }
}

Note that in SwiftUI 2 you can use control flow statements (like if-let ) directly in @ViewBuilder blocks:请注意,在 SwiftUI 2 中,您可以直接在@ViewBuilder块中使用控制流语句(如if-let ):

if let expensesList = expensesList {
    List {
        ForEach(expensesList) { log in
            // ...
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 使用 if 语句时:类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - While using an if statement: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 请帮忙:类型'()'不能符合'View'; 只有结构/枚举/类类型可以符合协议 - Please help: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 如何修复类型“()”不能符合“视图”; 只有结构/枚举/类类型可以符合协议 - how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols type() 不能符合 View; 只有结构/枚举/类类型可以符合协议 - Type () cannot conform to View; only struct/enum/class types can conform to protocols 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型“MovieSearchContainer.Type”不能符合“Decodable”; 只有结构/枚举/类类型可以符合协议 - Type 'MovieSearchContainer.Type' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols 协议类型'*'的值不能符合'*'; 只有结构/枚举/类类型可以符合协议 - Value of protocol type '*' cannot conform to '*'; only struct/enum/class types can conform to protocols Swift 协议类型“XXX”的值不能符合“可识别”; 只有结构/枚举/类类型可以符合协议 - Swift Value of protocol type 'XXX' cannot conform to 'Identifiable'; only struct/enum/class types can conform to protocols 协议类型“Encodable”的值不能符合“Encodable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM