I understand that __block in ARC retains the variable. This can then be used when accessing a variable within a block before the variable has been assigned, as in:
__block __weak id observer = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:player queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notif){
// reference the observer here. observer also retains this block,
// so we'd have a retain cycle unless we either nil out observer here OR
// unless we use __weak in addition to __block. But what does the latter mean?
}];
But I am having trouble parsing this. If __block causes the observer to be retained by the block, then what does it mean to effectively be both strong and weak? What is the __weak doing here?
__blockmeans that the variable is like global, that survives the current frame stack, and accessible by the block that you will declare in the scope.__weakmeans that the variable doesn’t retain the pointed object, but if the object gets deallocated the__weakpointer will be set to nil.In your case observer catches the return value of addObserverForName:object:queue:usingBlock: , so it doesn’t need to be strong. And if was strong then it was retaining the observer, making it global and surviving until the strong reference was set to nil.
Example
This example prints (null), let’s see what happened:
So you could just do it like this:
There’s no problem: str is strong by default so it’s captured, you don’t need the __block specifier.