Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using #define to access App Delegate Object doesn't work

I am using App Delegate object in different classes. I want access it in whole project. Am define this object in Prefix.pch file as

#define Appdelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]

but problem is that Appdelegate variable does not access App delegate variable. it Shows error.

but if am use this code works fine

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.variablename;

Am I doing it correct or is there a way of doing what I do?

thanks in Advance.

like image 899
Bond Avatar asked May 04 '12 11:05

Bond


People also ask

What is the synonym of using?

The words employ and utilize are common synonyms of use. While all three words mean "to put into service especially to attain an end," use implies availing oneself of something as a means or instrument to an end. willing to use any means to achieve her ends.

What is mean by using?

transitive verb. 1 : to put into action or service : avail oneself of : employ. 2 : to expend or consume by putting to use —often used with up. 3 : stand sense 1d the house could use a coat of paint. 4 : to consume or take (liquor, drugs, etc.)

What is the verb of using?

verb (used with object), used, us·ing. to employ for some purpose; put into service; make use of: to use a knife. to avail oneself of; apply to one's own purposes: to use the facilities. to expend or consume in use: We have used the money provided.

What is an example of use?

Use is the acting of employing or utilizing something or the intended purpose of something. An example of use is the act of hammering with a hammer and nails. An example of use is communication to the Internet. Use is defined as to handle or consume something.


2 Answers

Should be:

#define Appdelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
//                  ^----------------------parenthesis--------------------------^
like image 54
MByD Avatar answered Nov 16 '22 01:11

MByD


As this question and its answers demonstrate, leaning on the preprocessor is usually a bad idea.

  1. It's easy to get things wrong. We all develop good instincts for operator precedence, but the preprocessor has no clue. To defend against unexpected context problems, you often need to wrap everything in extra parens and braces.

  2. It's easy to convince yourself that you're doing one thing when the code is doing another. When things break, neither the compiler's error messages nor the debugger are likely to help much.

  3. Most important, the preprocessor can let you take a bad idea and spread it pervasively through the program. The bad idea here is using the App Delegate globally.

Global variables are a code smell. Efforts to make global variables even more global by stuffing them into precompiled headers are a code smell. If the framework thought you should have access to the AppDelegate from everywhere, you wouldn't need to jump through these (modest) hoops!

So, when tempted to do something like this, it's nice to know the preprocessor is there and the pch headers are there, but keep in mind that you're fighting the framework and almost certainly making a design error. That might be OK in your context, but it's something to know.

like image 32
Mark Bernstein Avatar answered Nov 16 '22 00:11

Mark Bernstein