ios – Objective-C用({…})声明vars
栏目: Objective-C · 发布时间: 6年前
内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/19732070/objective-c-declare-vars-with
我正在查看 REMenu lib
代码,看到一个vars被声明为wiht({…});看起来像“闭包”懒惰的评估代码..我不知道..有人可以解释我吗?
self.menuWrapperView = ({ UIView *view = [[UIView alloc] init]; view.autoresizingMask = UIViewAutoresizingFlexibleWidth; if (!self.liveBlur || !REUIKitIsFlatMode()) { view.layer.shadowColor = self.shadowColor.CGColor; view.layer.shadowOffset = self.shadowOffset; view.layer.shadowOpacity = self.shadowOpacity; view.layer.shadowRadius = self.shadowRadius; view.layer.shouldRasterize = YES; view.layer.rasterizationScale = [UIScreen mainScreen].scale; } view; }); self.toolbar = ({ UIToolbar *toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = self.liveBlurBackgroundStyle; if ([toolbar respondsToSelector:@selector(setBarTintColor:)]) [toolbar performSelector:@selector(setBarTintColor:) withObject:self.liveBlurTintColor]; toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth; toolbar; });
这是一个名为“ statement expression
”的GNU(非标准)C语言扩展. gcc,clang和 several other compilers
支持语法.
基本上,它允许您将任意块视为单个表达式,其值为块中最后一个语句的值.
这个扩展主要是宏定义.在我看来,您在问题中引用的代码(来自REMenu.m中的showFromRect:inView:method)会更好,如果它不使用语句表达式.相反,这些语句表达式中的代码应该被分解为单独的方法.例如:
self.menuWrapperView = [self newMenuWrapperView]; self.toolbar = [self newToolbar]; ... - (UIView *)newMenuWrapperView { UIView *view = [[UIView alloc] init]; view.autoresizingMask = UIViewAutoresizingFlexibleWidth; if (!self.liveBlur || !REUIKitIsFlatMode()) { view.layer.shadowColor = self.shadowColor.CGColor; view.layer.shadowOffset = self.shadowOffset; view.layer.shadowOpacity = self.shadowOpacity; view.layer.shadowRadius = self.shadowRadius; view.layer.shouldRasterize = YES; view.layer.rasterizationScale = [UIScreen mainScreen].scale; } return view; } - (UIToolbar *)newToolbar { UIToolbar *toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = self.liveBlurBackgroundStyle; if ([toolbar respondsToSelector:@selector(setBarTintColor:)]) [toolbar performSelector:@selector(setBarTintColor:) withObject:self.liveBlurTintColor]; toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth; return toolbar; }
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/19732070/objective-c-declare-vars-with
以上所述就是小编给大家介绍的《ios – Objective-C用({…})声明vars》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。