Reputation: 97
I want to use these lines of code from this source
#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)
#define TEXTVIEW_GET_HTML_TEXT(__textView__, __text__)\
do\
{\
if ([__textView__ respondsToSelector: NSSelectorFromString(@"contentAsHTMLString")])\
__text__ = [__textView__ performSelector: NSSelectorFromString(@"contentAsHTMLString") withObject: nil];\
else\
__text__ = __textView__.text;\
}\
while (0)
What should I do? I am new to macros. Should i define a uitextview variable with name __textView__
?? Is it possible to help me with some basic steps in order to use this code?
Upvotes: 1
Views: 381
Reputation: 3975
You just have to put your macros outside your @implementation
, something like this:
#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
You don't have to declare the variables, as these are already declared in the macro. You can think of this:
#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)
as this C-style function:
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);
}
The difference is that if you have declared it as a C-style function, it would be included in your app when it is compiled/linked. However, since it was #define
d, it means the compiler would change it first to the do-while
before compiling.
You would call it like this:
- (void)yourMethodThatWillChangeTheText
{
// ...
TEXTVIEW_SET_HTML_TEXT(self.myTextView, @"Hello");
// ...
}
As an additional info, #define
is usually used to define constants, something like:
#define PI_VALUE 3.141592
which would have to be called something like:
double circumference = 2 * PI_VALUE * radius;
But as seen in the macro, it can also look/be used as a function. Thus, you have to consider how the macro/#define
looks like to be sure that you call it properly.
Upvotes: 3