Objective c 目标c宏
我想使用这些代码行Objective c 目标c宏,objective-c,Objective C,我想使用这些代码行 我该怎么办?我不熟悉宏。我是否应该定义一个名为\uuuuuuTextView\uuuuuu的uitextview变量??是否可以帮助我执行一些基本步骤以使用此代码?您只需将宏放在@实现之外,类似于以下内容: #import "..." // Put the macros here // This block may or may not be present in your code... @interface YourClass () @end // ... up t
我该怎么办?我不熟悉宏。我是否应该定义一个名为
\uuuuuuTextView\uuuuuu
的uitextview变量??是否可以帮助我执行一些基本步骤以使用此代码?您只需将宏放在@实现之外,类似于以下内容:
#import "..."
// Put the macros here
// This block may or may not be present in your code...
@interface YourClass ()
@end
// ... up to here.
@implementation YourClass
@end
- (void)yourMethodThatWillChangeTheText
{
// ...
TEXTVIEW_SET_HTML_TEXT(self.myTextView, @"Hello");
// ...
}
您不必声明变量,因为这些变量已在宏中声明。你可以这样想:
#define TEXTVIEW_SET_HTML_TEXT(__textView__, __text__)\
do\
{\
if ([__textView__ respondsToSelector: NSSelectorFromString(@"setContentToHTMLString:")])\
[__textView__ performSelector: NSSelectorFromString(@"setContentToHTMLString:") withObject: __text__];\
else\
__textView__.text = __text__;\
}\
while (0)
作为此C型功能:
void TEXTVIEW_SET_HTML_TEXT(UITextView *__textView__, NSString *__text__)
{
do
{
if ([__textView__ respondsToSelector: NSSelectorFromString(@"setContentToHTMLString:")])
[__textView__ performSelector: NSSelectorFromString(@"setContentToHTMLString:") withObject: __text__];
else
__textView__.text = __text__;
}
while (0);
}
区别在于,如果您将其声明为C风格函数,则在编译/链接时,它将包含在应用程序中。但是,由于它是#define
d,这意味着编译器在编译之前会先将其更改为do,而
你可以这样称呼它:
#import "..."
// Put the macros here
// This block may or may not be present in your code...
@interface YourClass ()
@end
// ... up to here.
@implementation YourClass
@end
- (void)yourMethodThatWillChangeTheText
{
// ...
TEXTVIEW_SET_HTML_TEXT(self.myTextView, @"Hello");
// ...
}
作为附加信息,#define
通常用于定义常量,例如:
#define PI_VALUE 3.141592
double circumference = 2 * PI_VALUE * radius;
必须称之为:
#define PI_VALUE 3.141592
double circumference = 2 * PI_VALUE * radius;
但正如在宏中看到的,它也可以用作函数。因此,您必须考虑宏/<代码>定义的如何确保您正确调用它。< /P>