Reputation: 26223
I have just been looking at some old code and it got me thinking about which of these to use, both seem to work without complaint. I was just curious when I spotted the difference.
THIS:
id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
OR
id appDelegate = [[UIApplication sharedApplication] delegate];
Upvotes: 0
Views: 97
Reputation: 162722
While meronix's answer is correct, it misses an important point.
You should always declare variables to have the most specific type possible.
By doing so, you give the compiler the maximum amount of information with which to validate your code. Thus, this is preferable because it tells the compiler to limit the search for selectors to a minimal number:
id <UIApplicationDelegate> appDelegate = ...;
Note that id<SomeProtocol>
limits the set of valid selectors to only those that exist in SomeProtocol
. This why you'll sometimes see the protocol declared as also implementing <NSObject>
or you'll see id<SomeProtocol, NSObject>
(or NSObject<SomeProtocol>*
) as the type declaration.
Upvotes: 2
Reputation: 4249
Try this..
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication]delegate];
to avoid any kind of warning
Upvotes: 0
Reputation: 6176
i guess it depends on what you are asking to appDelegate
i mean, if you are going use the property "window" which is defined in UIApplicationDelegate protocol:
NSLog(@"%@", appDelegate.window);
then you should use :
id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
but if you try:
id appDelegate = [[UIApplication sharedApplication] delegate];
NSLog(@"%@", appDelegate.window);
you'll get an error...
Upvotes: 2