访问RxSwift内的项目

时间:2017-04-06 18:33:27

标签: ios rx-swift

我正在使用RxSwift,我正在尝试访问我的订阅功能中的各个项目。但是.subscribe给了我ShoppingList的数组,我想要一个单独的ShoppingList项目。

private var disposeBag = DisposeBag()

var shoppingLists :Variable<[ShoppingList]> = Variable([])

override func viewDidLoad() {
    super.viewDidLoad()

    // add to the shoppinglist
    let shoppingList = ShoppingList(title: "Walmart")
    self.shoppingLists.value.append(shoppingList)


    shoppingLists.asObservable()
       .subscribe { shoppingLists in

            // how to access the item inside the shoppingList // Walmart

            print("subscribe")
            print(shoppingLists)
        }
        .addDisposableTo(disposeBag)


}

1 个答案:

答案 0 :(得分:1)

shoppingLists.asObservable()
   .flatMap { shoppingList in
       Observable.from(shoppingList) // <- magic here
   }
   .subscribe(onNext: { item in
        print("subscribe")
        print(item)
    })
    .addDisposableTo(disposeBag)