This does not result in a read-only variable assignment error:
NSURL *pathToFile = nil; [oPanel beginSheetModalForWindow:theWindow completionHandler:^(NSInteger returnCode) { if (returnCode == NSOKButton) pathToFile = [[oPanel URLs] objectAtIndex:0]; }]; return pathToFile;
Yes, because you are trying to assign a copy to the pathToFile variable that is created when the block is created. You do not assign the original variable pathToFile that you declared outside the block.
You can use the __block keyword so that the block __block this variable, but I donβt think it will help, because beginSheetModalForWindow:completionHandler: not blocked. (This is not mentioned in the documentation, but there is no reason to block the method, and you can check with a log that this is not so.) The message is returned immediately while the panel is still working.
So, you are trying to assign a handler-completion block to a local variable, but your method in which you declared a local variable will probably be returned by running the temporary block, so it will not be able to work with the value that remains in the variable left .
Whatever you do with pathToFile , there must be either in the block itself or in the method (taking the argument NSURL * ) that the block can call.
source share