I saw at the some code from iOS developer. It has some code following:
void (^block)(NSUInteger buttonIndex, UIAlertView *alertView) = objc_getAssociatedObject(self, "blockCallback");
But. I don’t understand why assign object self to block code. The full code of the method is:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
void (^block)(NSUInteger buttonIndex, UIAlertView *alertView) = objc_getAssociatedObject(self, "blockCallback");
if (block) {
block(buttonIndex, self);
}
}
A category is being used to add block-based functionality to a UIAlertView. This means you don’t have to implement delegate methods for the alert view, you can give it blocks at creation time instead.
However, categories don’t allow you to add instance variables to an object. In this case, the blocks need to be stored so that they can be executed later when the user hits a button on the alert view.
To get around this, the programmer has used associated objects, which allow you to add pseudo-ivars when writing category code. Search for objective-c associated objects for more information.