Macros for Objective-C

In Objective C
#define IS_PAD [[UIDevice currentDevice] interfaceIdiom] == UIUserInterfaceIdiomPad)
Now check 
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 MacroFunction
1Macro is PreprocessedFunction is Compiled
2No Type CheckingType Checking is Done
3Code Length IncreasesCode Length remains Same
4Use of macro can lead to side effectNo side Effect
-
5Speed of Execution is FasterSpeed of Execution is Slower
6Before Compilation macro name is replaced by macro valueDuring function call , Transfer of Control takes place
7Useful where small code appears many timeUseful where large code appears many time
8Generally Macros do not extend beyond one lineFunction can be of any number of lines
9Macro does not Check Compile ErrorsFunction 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

Popular posts from this blog

Launch iPhone simulator without Xcode.

How to Make REST api call in Objective-C.

id vs NSObject