Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 将参数传递给包含块和赋值的方法_Ios_Objective C_Objective C Blocks - Fatal编程技术网

Ios 将参数传递给包含块和赋值的方法

Ios 将参数传递给包含块和赋值的方法,ios,objective-c,objective-c-blocks,Ios,Objective C,Objective C Blocks,我有一个包含动画块的方法。我试图将一个类属性作为参数传递给该方法,并在块内为其赋值为nil -(void)endPinching:(UIViewController *)pinchedController{ // a bunch of code [UIView animateWithDuration:0.2 animations:^{ //do stuff } completion:^(BOOL finished) { // do o

我有一个包含动画块的方法。我试图将一个类属性作为参数传递给该方法,并在块内为其赋值为nil

-(void)endPinching:(UIViewController *)pinchedController{

    // a bunch of code

    [UIView animateWithDuration:0.2 animations:^{
        //do stuff

    } completion:^(BOOL finished) {

       // do other stuff

        pinchedController = nil; //HERE IS THE PROBLEM!!
    }];
  }
}
因此,如果我在调用该方法的类中调用该方法,pinchController是self.pinchController,因此可以在块内设置为nil。但在这里,在定义中,我得到了一个编译器错误:

"variable not assignable, missing block type specifier"
因此,我尝试通过以下操作添加块说明符:

__block pinchedController = nil;
编译器说:

"unused variable pinchedViewController"
我假设这意味着pinchedController现在被视为一个新变量,并且与方法参数无关


我的问题是:是否有方法将PincheViewController作为参数传入并在该块中将其分配给nil?

如果我理解正确,您希望显式解除分配给定对象。这是不可能的,因为引用计数是如何工作的

通过将
UIViewController
传递到
endPinching:
,可以将对象的refcount增加1。将指针(堆栈上的一个局部变量)设置为
nil
只会将引用计数减少1,但由于这仍然是类的一个实例变量,因此它不会被释放

因此,首先,您应该问问自己,为什么要取消分配这样的对象。这可以通过更好地设计代码或试图找出视图控制器占用大量内存的原因来解决


如果仍要取消分配,可以在方法中显式设置
self.pinchController=nil
,或者调用某种类型的委托来实现这一点(我需要看到整个视图控制器设计更加具体)。

;在本例中,
pinchedController
是该方法的一个参数,该参数将非常长,直到运行完成块时才消失(因为动画是异步的)。因此,该参数中的
pinchedController
隐含的强引用已经消失。如果我没有弄错的话,完成块应该包含对其所有闭包的强引用,同时捕获
pinchedController
。否?是的,但该参考是保留副本。
pinchedController
参数/局部变量早已消失,因为
endPinching:
方法早在执行完成框之前就返回了。哦,当然。我想说的是,将
nil
设置为堆栈变量(不管它是函数的堆栈还是块的堆栈)不起作用。设置局部变量(在这种情况下设置为
nil
)然后不再使用它有什么意义?