Reputation: 6749
I come from c++/c#/java camp and am confused when I see the following objective-c syntax...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions { }
from what I understand it goes (return type)functionName:(param type)param; like the following
- (void)applicationWillResignActive:(UIApplication *)application { }
whats with the parameter (UIApplication *)application didFinishLaunchingWithOptions?
Upvotes: 4
Views: 328
Reputation: 811
This is what helped me get it. All the parameters are named. The weirdness is because your first parameter name is really part of the function name, OR, you can think of it as: there really is no first parameter name. Many times you'll see or write functions that look like this:
-(returntype) SomeFunctionWithSuperParameterComingUpNext:(paramtype)param1 SuperParam2:(paramtype2)param2;
Notice, the nonsense description at the end of the function name, describing the param. ;-)
This really twisted me up early on, but you get used to it rather quickly, and you'll actually wind up missing it when using other languages. :-)
Hope that helps, and have fun!!
Upvotes: 3
Reputation: 9820
the didFinishLaucnhingWithOptions: part is an identifier for another paramater, launchOptions--which is a NSDictionary*--, so instead of having a comma inbetween paramaters (java) its a space then descriptor then colon then paramater.However, the actual method signature includes these identifiers or descriptors.
Edit: see @pgb answer for a better description.
It makes code WAY easier to read, and makes it harder for programmers to have crappy variable names like a, b, c, foo,bar, etc. where you have no idea what they are or do.
Example:
-(returntype) myFunction:(paramater1type)paramater1 paramater2descriptor:(paramater2type)paramater2 paramater3descriptor:(paramater3type)paramter3 {}
Upvotes: 1
Reputation: 25001
In Objective-C the parameters are part of the method signature. The selector for the method you describe would be application:didFinishLaunchingWithOptions:
. This comes from Smalltalk, and while it may make the method declaration harder to read, it makes the code actually easy to read:
id anApplication;
id someOptions;
[delegate application:anApplication didFinishLaunchingWithOptions:someOptions];
As you can see, the resulting calling code looks as if you were reading a phrase.
As for the UIApplication
parameter, that's a design choice you'll see throughout Cocoa. All the methods in a delegate will receive as its first parameter the object they are delegates of. This makes allows you to reuse a delegate, and have its logic depend on the object they are the delegate of.
In this case, you could use the same UIApplicationDelegate
for different UIApplication
instances, and have its code be conditional based on some UIApplication
parameters.
Upvotes: 3
Reputation: 28688
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions { }
Is broken up kind of oddly.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }
would make it a little easier to read I guess. there are two params here, application and launchOptions.
Upvotes: 0