首页 新闻 赞助 找找看

请问各位大神,在OC中,这是个什么语法格式。

0
悬赏园豆:40 [待解决问题]

NSString *str = ({
            @"nihao";
        });

项目中可以这样用:

 

_cateButton = ({

            UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

            button.frame = CGRectMake(10, _carButton.frame.origin.y + _carButton.frame.size.height, 100, 15);

            button.backgroundColor = [UIColorcolorWithWhite:1.000alpha:0.750];

             button.titleLabel.font = [UIFontsystemFontOfSize:14 * (SCREEN_WIDTH / 375)];

            button.opaque = YES;

            button;

        });

白痴的救赎的主页 白痴的救赎 | 初学一级 | 园豆:162
提问于:2016-06-21 11:02
< >
分享
所有回答(1)
0

这样写没有意义吧?  ({  @"nihao";}) ;里面有分号,还是检测里面的字符串, a = ((2))  这种的多加这么多括号没有什么意义啊

出神入化VV | 园豆:217 (菜鸟二级) | 2016-08-22 13:58

感谢你的回复。

后来有大神告诉我了,只是c语言的一种复合语法格式。({})这里面包裹的最后一句代码就是返回值。

_cateButton = ({

            UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

            button.frame = CGRectMake(10, _carButton.frame.origin.y + _carButton.frame.size.height, 100,15);

            button.backgroundColor = [UIColorcolorWithWhite:1.000alpha:0.750];

             button.titleLabel.font = [UIFontsystemFontOfSize:14 * (SCREEN_WIDTH / 375)];

            button.opaque = YES;

            button;

        });

 

 就像这里面的最后一句代码是不能删除的。否则语法错误。
这样可以在写懒加载时代码简化一点。
支持(0) 反对(0) 白痴的救赎 | 园豆:162 (初学一级) | 2016-08-22 14:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册