简体   繁体   English

objective-c 语法问题

[英]objective-c syntax question

I come from c++/c#/java camp and am confused when I see the following objective-c syntax...我来自 c++/c#/java 阵营,当我看到以下 objective-c 语法时感到困惑......

- (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?参数 (UIApplication *)application didFinishLaunchingWithOptions 是怎么回事?

In Objective-C the parameters are part of the method signature.在 Objective-C 中,参数是方法签名的一部分。 The selector for the method you describe would be application:didFinishLaunchingWithOptions: .您描述的方法的选择器将是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:这来自Smalltalk ,虽然它可能使方法声明更难阅读,但它使代码实际上易于阅读:

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.至于UIApplication参数,这是您将在整个 Cocoa 中看到的设计选择。 All the methods in a delegate will receive as its first parameter the object they are delegates of.委托中的所有方法都将接收作为其委托的 object 作为其第一个参数。 This makes allows you to reuse a delegate, and have its logic depend on the object they are the delegate of.这使得您可以重用委托,并使其逻辑依赖于它们是委托的 object。

In this case, you could use the same UIApplicationDelegate for different UIApplication instances, and have its code be conditional based on some UIApplication parameters.在这种情况下,您可以将相同的UIApplicationDelegate用于不同的UIApplication实例,并使其代码基于某些UIApplication参数有条件。

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.奇怪的是,您的第一个参数名称确实是 function 名称的一部分,或者,您可以将其视为:确实没有第一个参数名称。 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.请注意,function 名称末尾的废话描述,描述了参数。 ;-) ;-)

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!!希望有帮助,玩得开心!!

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. didFinishLaucnhingWithOptions: 部分是另一个参数的标识符,launchOptions--这是一个 NSDictionary*--,所以不是在参数(java)之间有逗号,而是一个空格,然后是描述符,然后是冒号,然后是参数。但是,实际的方法签名包括这些标识符或描述符。

Edit: see @pgb answer for a better description.编辑:请参阅@pgb 答案以获得更好的描述。

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.它使代码更易于阅读,并使程序员更难在不知道它们是什么或做什么的地方使用诸如 a、b、c、foo、bar 等蹩脚的变量名称。

Example:例子:

-(returntype) myFunction:(paramater1type)paramater1 paramater2descriptor:(paramater2type)paramater2 paramater3descriptor:(paramater3type)paramter3 {}
- (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.这里有两个参数,application 和launchOptions。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM