Question

I am trying to convert an old non-ARC project to ARC and I am getting this compilation error: "cannot capture __autoreleasing variable in a block"

- (void)animateViewController:(__autoreleasing animatingViewController *)viewController 
{
   //[[viewController retain] autorelease]; // I replaced this with __autoreleasing

        [UIView animateWithDuration:0.14 animations:^{
            [[viewController view] setAlpha:0.0];
        } completion:^(BOOL finished) {
            [viewController.view removeFromSuperView];
        }];
}
Was it helpful?

Solution

As the block captures and retains the viewController parameter, it's not necessary to retain-autorelease the object. The lifetime is extended until the animation finishes because the completion block holds on to the controller.

Just remove the __autoreleasing specifier.

If, in another scenario, you really have to retain-autorelease an instance, you could assign it to an id __autoreleasing __attribute__((unused)) local variable. But this should be a very uncommon case and might be a sign of a flaw in your design.

OTHER TIPS

__autoreleasing is almost never used. __autoreleasing is mainly only important in a "pointer to __autoreleasing", i.e. id __autoreleasing *, or NSString * __autoreleasing *. In that case, it is different from "pointer to __strong", i.e. id *.

In your case, you have an __autoreleasing local variable directly. There is no benefit of this over __strong (if you don't put any qualifier, it is implicitly __strong), and is in fact worse. __strong will retain and release correctly as needed; not needing to use autorelease if there is no need.

It is possible to disable ARC for individual files by adding the -fno-objc-arc compiler flag for those files.

You add compiler flags in Targets -> Build Phases -> Compile Sources. You have to double click on the right column of the row under Compiler Flags. You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top