Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获得;在key value Observators仍在其注册时进行了交易。”;转换为圆弧后的错误_Ios_Cocoa Touch_Observer Pattern_Dealloc_Automatic Ref Counting - Fatal编程技术网

Ios 获得;在key value Observators仍在其注册时进行了交易。”;转换为圆弧后的错误

Ios 获得;在key value Observators仍在其注册时进行了交易。”;转换为圆弧后的错误,ios,cocoa-touch,observer-pattern,dealloc,automatic-ref-counting,Ios,Cocoa Touch,Observer Pattern,Dealloc,Automatic Ref Counting,我正在使用这个类: 由于我转换为ARC,我在点击pickerview几次后出现此错误: 2011-10-18 14:10:19.424 MappingApp[3398:10d03] An instance 0x73c7cd0 of class CustomTapGestureRecognizer was deallocated while key value observers were still registered with it. Observation info was leaked

我正在使用这个类:

由于我转换为ARC,我在点击pickerview几次后出现此错误:

2011-10-18 14:10:19.424 MappingApp[3398:10d03] An instance 0x73c7cd0 of class CustomTapGestureRecognizer was deallocated while key value observers were still registered with it. Observation info was leaked, and may even become mistakenly attached to some other object. Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info: <NSKeyValueObservationInfo 0x5d93430> (<NSKeyValueObservance 0x5d933f0: Observer: 0x5d66eb0, Key path: state, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x746b180>)
在checkview中,我有以下方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    // Simple hack, set recognizer state to possible as tap begins
    self.state = UIGestureRecognizerStatePossible;
}
- (void)didMoveToSuperview {
    gestureRec = [[CustomTapGestureRecognizer alloc] initWithTarget:nil action:nil];
    gestureRec.numberOfTapsRequired = 1;
    [gestureRec addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
    [[self superview] addGestureRecognizer:gestureRec];
}

而且,我知道可能导致此问题的removeObserver位于checkview的dealloc中。我应该把这个搬到别的地方吗?有没有人知道是什么导致了这个问题?这在ARC之前从未发生过。

我猜
CheckView
类中的方法
didmovetoserview
被多次调用,导致
gestureRec
实例变量被重新分配,并且先前的
customTapgestureRecognitor
实例被认为没有ARC留下的引用,然后被解除分配(导致警告消息显示有人仍在观察该实例)

尝试添加
NSLog(@“didMoveToSuperview:self=%@gestureRec=%@”,self,gestureRec)
didMoveToSuperview
的开头,查看是否存在这种情况

如果是这样的话,快速修复可能是这样的,但我自己没有尝试过,也不太了解代码

- (void)didMoveToSuperview {
  if (gestureRec != nil) {
    [gestureRec removeObserver:self forKeyPath:@"state"];
  }
  gestureRec = [[CustomTapGestureRecognizer alloc] initWithTarget:nil action:nil];
  gestureRec.numberOfTapsRequired = 1;
  [gestureRec addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
  [[self superview] addGestureRecognizer:gestureRec];   
}

美好的双赢,因为ARC可能也为您修复了一个内存泄漏:)此外,您还应该在上游版本中进行fork和do pull请求来修复此问题,如果这是一个很好的修复方法,可能会得到一些反馈。