簡體   English   中英

Swift 無法推斷復雜的閉包返回類型

[英]Swift unable to infer complex closure return type

我得到了我在標題中聲明的錯誤,它似乎在 HStack 中。

我的代碼:

import SwiftUI
import SDWebImageSwiftUI
import FirebaseFirestore

struct CartView : View {
    
    @ObservedObject var cartdata = getCartData()
    @State private var showRow = true
    
    
    var body : some View{
        
        VStack(alignment: .leading){
            
            Text(self.cartdata.datas.count != 0 ? "Items In The Cart" : "No Items In Cart").padding([.top,.leading])
            
            
            if self.cartdata.datas.count != 0{
                
                List{
                    
                    
                    ForEach(self.cartdata.datas){i in
                        
                        
                        if i.quantity == 0 {
                            let db = Firestore.firestore()
                            db.collection("cart").document(self.cartdata.datas[i.last!].id).delete { (err) in
                                
                                if err != nil{
                                    print((err?.localizedDescription)!)
                                    return
                                }
                                
                                self.cartdata.datas.remove(atOffsets: i)
                            }
                            
                        }
                        
                        
                        HStack(spacing: 15){
                            
                            CartRow(item: i)
                                .onTapGesture {
                                    UIApplication.shared.windows.last?.rootViewController?.present(textFieldAlertView(id: i.id), animated: true, completion: nil)
                            }
                            
                        }
                    }
                    .onDelete { (index) in
                        
                        let db = Firestore.firestore()
                        db.collection("cart").document(self.cartdata.datas[index.last!].id).delete { (err) in
                            
                            if err != nil{
                                print((err?.localizedDescription)!)
                                return
                            }
                            
                            self.cartdata.datas.remove(atOffsets: index)
                        }
                    }
                    
                }
            }
            

我想要做的是,如果項目的數量等於 0,它會從數據庫中刪除並從我的視圖中刪除。

該數據庫是 Firestore 數據庫。 self.carddata.datas 是它從 Firestore 數據庫中讀取的項目數組。

您不能在 ForEach 的 View 中使用該 if 和 action。

您可以在 ForEach 內的 HStack 的.onAppear視圖修飾符中使用它,具體取決於您的用例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM