代码之家  ›  专栏  ›  技术社区  ›  Mohamed ALOUANE

使用rxswift的多重映射

  •  0
  • Mohamed ALOUANE  · 技术社区  · 7 年前

    我试图从项目数组中为tableview创建节,其中每个数组项目都是一个节项目。

    我的问题是我怎样才能 cart.map

    我的代码 让购物车:行为休闲 结构输出{ let项目:可观察<[CartProductSection]>

               var sectionPerSupplierItems: [CartProductSection] = []
    
                _ = cart.map { (cart)  in 
                    let cartItemsPerSupplier = self.sortModelItemPerSection(items: cart.items)
    
                    print(cart.items.count)
                     _ = cartItemsPerSupplier.map {  (cartItemsPerSupplier) in
    
                        var items: [CartProductSectionItem] = []
                        for cartItem in cartItemsPerSupplier.cartItems {
                             items.append(CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: cartItem)))
                        }
    
                        sectionPerSupplierItems.append(CartProductSection.cartItemModel
    (title: cartItemsPerSupplier.companyName, items: items))
                    }
                }
    
                return Output(items: Observable.just(sectionPerSupplierItems))
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Daniel T.    7 年前

    我可能会这样写代码(我还在这里做一大堆假设):

    let items = cart
        .map { [unowned self] in self.sortModelItemPerSection(items: $0.items) }
        .map { cartItemsPerSuppliers in
            cartItemsPerSuppliers.map { CartProductSection.cartItemModel(from: $0) }
        }
    return Output(items: items)
    

    以上假设存在类似的情况:

    extension CartProductSection {
        static func cartItemModel(from supplier: CartItemsPerSupplier) -> CartProductSection {
            return cartItemModel(title: supplier.companyName, items: supplier.cartItems.map { CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: $0)) })
        }
    }
    
    推荐文章