0
- (void)setTableView:(UITableView *)tableView {
  _tableView = tableView;
  [_tableView addObserver:self
               forKeyPath:@"frame"
                  options:0
                  context:nil];
  [self updateFrame];
}

The exc_bad_access occurs when trying to add an observer.

In the assembly code, the error code is 'NSKeyValueObserverRegistrationLock'.

Have no idea what's causing the error.

I'm running XCTest so there might be a possibility that error was cause by injecting the test code into application code.

Anybody helps?

riven
  • 73
  • 9

1 Answers1

0

A couple of things.

You should define your options parameter. The NSKeyValueObservingOptions struct doesn't have an entry for 0. If you are after the new value then use NSKeyValueObservingOptionNew.

Next I assume the function you have listed resides in a UIViewController? UIViewController doesn't have a frame property. It's view does though (so does your tableView). I'm not sure which frame you're trying to observe, but you can try:

  [_tableView addObserver:self.view
               forKeyPath:@"frame"
                  options:NSKeyValueObservingOptionNew
                  context:NULL];

Or

  [_tableView addObserver:tableView
               forKeyPath:@"frame"
                  options:NSKeyValueObservingOptionNew
                  context:NULL];
VaporwareWolf
  • 10,143
  • 10
  • 54
  • 80
  • "The NSKeyValueObservingOptions struct doesn't have an entry for 0." - True, but Apple's documentation for [NSKeyValueObservingOptions](https://developer.apple.com/documentation/foundation/nskeyvalueobservingoptions?language=objc) says this: "You can pass 0 if you require no change dictionary values". – herzbube Mar 12 '21 at 16:59
  • herzbube Good catch. – VaporwareWolf Mar 19 '21 at 16:38