“宏"是什么意思?在Objective-C中意味着什么? [英] What does "macro" mean in Objective-C?

查看:108
本文介绍了“宏"是什么意思?在Objective-C中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手,我只想了解Objective-C中宏的含义?

I am new to iOS development and I just want to know the meaning of macro in Objective-C?

我发现#define使用了宏",但仍然没有含义.

I have found that "macro" is used with #define but still do not get its meaning.

http://www.saturngod.net/ios-macro-有条件定义值

推荐答案

宏是一段已命名的代码片段.每当使用该名称时,它就会被宏的内容替换.有两种宏.它们的主要区别在于使用时的外观.类对象宏在使用时类似于数据对象,类函数宏类似于函数调用.

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.

类对象宏是一个简单的标识符,它将被代码片段替换.之所以称为类对象,是因为它看起来像是使用它的代码中的数据对象.它们最常用于为数字常数赋予符号名称.

An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants.

您可以使用"#define"指令创建宏. "#define"后跟宏的名称,然后是令牌序列的缩写,它被不同地称为宏的主体,扩展或替换列表.例如,

You create macros with the ‘#define’ directive. ‘#define’ is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro's body, expansion or replacement list. For example,

 #define BUFFER_SIZE 1024

将名为BUFFER_SIZE的宏定义为令牌1024的缩写.如果在此"#define"指令之后的某处,将出现形式为

defines a macro named BUFFER_SIZE as an abbreviation for the token 1024. If somewhere after this ‘#define’ directive there comes a Objective C statement of the form

 foo = (char *) malloc (BUFFER_SIZE);

与编写

 foo = (char *) malloc (1024);

您还可以定义其用法类似于函数调用的宏.这些称为类函数宏.要定义类似函数的宏,请使用相同的"#define"指令,但是您必须在宏名称后紧跟一对括号. 像:

You can also define macros whose use looks like a function call. These are called function-like macros. To define a function-like macro, you use the same ‘#define’ directive, but you put a pair of parentheses immediately after the macro name. Like:

#define isIphone([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)

#define GetImage(imageName) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]     pathForResource:imageName ofType:@"png"]]

这篇关于“宏"是什么意思?在Objective-C中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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