我有一个关于声明块作为变量的最佳做法的问题。将Objective-C块声明为变量的最佳做法
最初我写了这样的我块变量:
id actionHandler = ^(UIAlertAction * action) {
// Handling code
};
供以后使用像这样:
UIAlertAction *action = [UIAlertAction actionWithTitle:@"Title"
style:UIAlertActionStyleDefault
handler:actionHandler];
但是,当我遇到Apple's Working With Blocks指导来了,我看见我可以重写它像这样:
void (^actionHandler)(UIAlertAction * action) = ^(UIAlertAction * action) {
// Handling code
};
这是“正确的”方式来声明它吗?这在我看来并不可读,但我对Objective-C没有太多的经验。那么将块声明为变量的最佳做法是什么?
编辑:好了,感谢所有的澄清!定义typedef
为shownamin-negm-awad和其他似乎也是一种很好的替代方法。
你也可以考虑使用'typedef'。参见例如http://stackoverflow.com/questions/8956921/setting-up-a-block-property-in-custom-class/8956967#8956967 –