为了清理我的代码,将#define
用于多行代码是一种好习惯吗?大多数例子是几行,定义尺寸等
在我的示例中,我有以下代码段的单独头文件。以这种方式使用#define
是一种好习惯吗?
#define KCHECKREFERENCE if([self.partClasses containsObject:@"Part1"]||[self.partClasses containsObject:@"Part2"]||[self.partClasses containsObject:@"Part3"]||[self.partClasses containsObject:@"Part4"]||[self.partClasses containsObject:@"Part5"]||[self.partClasses containsObject:@"Part6"]||[self.partClasses containsObject:@"ICDomesticEICPart7"]||[self.partClasses containsObject:@"ICDomesticEICPart8"] ){\
[self.currentPartView save];\
self.previousPartView = self.currentPartView;\
int nextPartNumber = 1;\
ICCertificateComponent *part = [self loadPart:nextPartNumber];\
self.currentPartView = part;\
CGRect nextPartViewFrame = self.currentPartView.view.frame;\
nextPartViewFrame.origin.x = 320.0f;\
self.currentPartView.view.frame = nextPartViewFrame;\
CGRect previousPartViewFrame = self.previousPartView.view.frame;\
previousPartViewFrame.origin.x = -320.0f;\
nextPartViewFrame.origin.x = 0;\
[self.view insertSubview:self.currentPartView.view belowSubview:self.navBarView];\
NSTimeInterval duration = 0.1;\
[UIView animateWithDuration:duration\
animations:^{\
self.previousPartView.view.frame = previousPartViewFrame;\
self.currentPartView.view.frame = nextPartViewFrame;\
}\
completion:^(BOOL finished) {\
self.currentPartNumber = 1;\
self.navBarView.prevButton.enabled = NO;\
self.navBarView.nextButton.enabled = YES;\
[self.previousPartView.view removeFromSuperview];\
self.previousPartView = nil;\
if (self.currentPartView.showsShareOptions == YES) {\
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Share" style:UIBarButtonItemStyleBordered target:self action:@selector(shareButtonPressed:)];\
}\
}\
];\
答案 0 :(得分:5)
没有。特别是对于那些特别的东西。
而不是#define
,将此代码放在方法中。
答案 1 :(得分:2)
我知道定义是良好做法的几种情况。例如_countof
宏
#if !defined(_countof)
#if !defined(__cplusplus)
#define _countof(_Array) (sizeof(_Array) / sizeof(0[_Array]))
#else
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))
#endif
#endif
但你的KCHECKREFERENCE是邪恶的。您应该使用inline
功能。为什么?至少因为你不能在你的宏中设置断点。