iOS创建宏 [英] iOS create macro

查看:347
本文介绍了iOS创建宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段我经常使用的代码,并希望用它制作一个宏。我不确定如何做到这一点。这是我想要使用的代码

I have a piece of code I'm using fairly often and would like to make a macro out of it. I'm not exactly sure how to do that though. Here's the code I want to use

UIImage *titleImage = [UIImage imageNamed:@"myLogo.png"];
UIImageView *titleImageView = [[UIImageView alloc] initWithImage:titleImage];
self.navigationItem.titleView = titleImageView;
[titleImageView release];

我想将此块定义为宏,以后我可以说例如addImage(... );谢谢你的帮助。

I want to define this block as a macro so I can later say for instance addImage(...); Thanks for your help.

推荐答案

#define MY_MACRO( img ) \
    {\
        UIImage *titleImage = [UIImage imageNamed:img]; \
        UIImageView *titleImageView = [[UIImageView alloc] initWithImage:titleImage]; \
        self.navigationItem.titleView = titleImageView; \
        [titleImageView release];\
    }

使用它像这个:

MY_MACRO( @"myLogo.png" )

使用 {} 创建一个范围块,这将防止变量重定义出现问题(如果你有变量)使用宏的名称相同。

The use of {} creates a scope block, which will prevent problems with variable redefinitions (if you have variables with the same name, where you use the macro).

这篇关于iOS创建宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆