简体   繁体   中英

KVO exc_bad_access (code=1)

- (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?

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM