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.
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.
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.)
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.
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.
Should be:
#define Appdelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])
// ^----------------------parenthesis--------------------------^
As this question and its answers demonstrate, leaning on the preprocessor is usually a bad idea.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With