Macros for Objective-C
In Objective C
Now check#define IS_PAD [[UIDevice currentDevice] interfaceIdiom] == UIUserInterfaceIdiomPad)
if(IS_PAD)
{
//iPad Stuff
}
else
{
//iPhone Stuff
}
Other useful Eg.
#define ALERT(title, message) [[[UIAlertView alloc] initWithTitle:title\ message:message\ delegate:nil\ cancelButtonTitle:@"OK"\ otherButtonTitles:nil] show]
#define GOBACK [self.navigationController popViewControllerAnimated:YES] #define DELEGATE_Is_OK(delegateMethod) [self.delegate respondsToSelector:@selector(yourDelegateMethod)]
________________________________________________________________________
Macros are preprocessor definitions. What this means is that before your code is compiled, the preprocessor scans your code and, amongst other things, substitutes the definition of your macro wherever it sees the name of your macro.(i.e. will replace the macro call with macro definition).
Suppose you want a method to return the maximum of two numbers. You write a macro to do this simple task:
#define MAX(x, y) x > y ? x : y
Simple, right? You then use the macro in your code like this:
int a = 1, b = 2;
int result = 3 + MAX(a, b);
________________________________________________________________________
Don’t Use Macros to Define Constants
Probably the most common macro use is to define a constant:
#define NUM_THREADS 4
Here it’s better to use a static const integer:
static const NSInteger NUM_THREADS = 4;
The advantages over the macro are that the value of the variable can be viewed in the debugger, it respects scope according to the rules of the language, and it’s type-safe.
________________________________________________________________________
Difference between macro and function
No Macro Function
1 Macro is Preprocessed Function is Compiled
2 No Type Checking Type Checking is Done
3 Code Length Increases Code Length remains Same
4 Use of macro can lead
to side effect No side Effect
-
5 Speed of Execution is
Faster Speed of Execution is Slower
6 Before Compilation macro name is replaced by macro value During function call , Transfer of Control takes place
7 Useful where small code appears many time Useful where large code appears many time
8 Generally Macros do not extend beyond one line Function can be of any number of lines
9 Macro does not Check Compile Errors Function Checks Compile Errors
Note - It iss better to use Macros, when the definition is very small in size.(because its faster) and It is better to use functions, when the definition is bigger in size. (because Functions make program size smaller and compact, If function is called 100 numbers of times, the program size will not increase.)
________________________________________________________________________
________________________________________________________________________
Related Other Links -
http://matt.coneybeare.me/my-favorite-macros-for-objective-c-development-in-xcode/
http://www.tutorialspoint.com/objective_c/objective_c_preprocessors.htm
http://qualitycoding.org/preprocessor/
Comments
Post a Comment