开发者

Unexpected EXC_BAD with NSNumber

I am trying to observe a BOOL property of an view. Because there are different kinds of view to observe, I check the selector and use performSelector to get value.

if( [tab respondsToSelector:@selector(canGoBack)] ) {
  [tab addObserver:self forKeyPath:@"canGoBack" 
    options:NSKeyValueObservingOptionNew 
    context:NULL];
  NSNumber* value = (NSNumber*)[tab performSelector:@selector(canGoBack)];
  canGoBack = [value boolValue];
}

However, I got EXC_BAD accidentally at "[value boolValue]". I checked the tab, it did exist 开发者_如何学Cwhen the error occurred.

After I changed the code to the following, the error has never been caught again.

  if( [tab respondsToSelector:@selector(canGoBack)] ) {
    [tab addObserver:self forKeyPath:@"canGoBack" 
      options:NSKeyValueObservingOptionNew 
      context:NULL];
      canGoBack = [tab canGoBack];
//    NSNumber* value = (NSNumber*)[tab performSelector:@selector(canGoBack)];
//    canGoBack = [value boolValue];
  }

I wonder, why?


because your method canGoBack returns a BOOL while performSelector: expects a return value of type id. the result of BOOL will not be an address of a valid objc object, which you cast it as, then use to send an objc message with.


canGoBack is selector method and you trying to assign a bool value to that method? how can it be possible?.. and second think ,are you sure the NSNumber you are getting is only 1 or 0, otherwise typecasting NSNumber t o bool is not use full


I think you using canGoBack as function and also as bool value. That's the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜