0

I want to change title with an observable int.

in view Model

var index = Variable<Int>(0)

in view Controller

let title = ["title1","title2","title3","title4","title5"]
override func viewWillAppear(_ animated: Bool) {
self.viewModel.index.value = 0
    self.viewModel.index
        .asObservable()
        .map( {self.periodText[$0]
        })
        .bind(to: self.titleLabel.rx.text)

        .addDisposableTo(self.disposeBag)
}

When i do this i have an error in the blind(to) :

fatal error: unexpectedly found nil while unwrapping an Optional value

The function never pass in the .map

How i can change the title when my index change in RX Swift?

user3333632
  • 51
  • 1
  • 4

2 Answers2

1

The textField on the storyBoard doesn't connect to the @IBOutlet titleLabel which results in this error message.

L. Guthardt
  • 1,990
  • 6
  • 22
  • 44
user3333632
  • 51
  • 1
  • 4
0

My guess is your self.periodText array does not have as many elements as your index value, thus running out of bounds.

Alternatively, it could be that you're defining your diposeBag as self.disposeBag: DisposeBag!. If you've then forgotten to create an instantiation of it before viewWillAppear, it is still set to nil, in which case you'll run into this error too.

Generally, it is best to share more details on where your error occurs, so it is easier to narrow it down.

RamwiseMatt
  • 2,717
  • 3
  • 16
  • 22