我正试图从一个项目数组中为tableview创建一个部分,其中每个数组项都是一个部分项。
我的任务是如何使cart.map返回一个数组
CartProductSection([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))

最佳答案

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

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)) })
    }
}

关于ios - 使用rxswift的多个 map ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53987750/

10-14 21:03
查看更多