Objective-C 中的方法语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/683211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:17:56  来源:igfitidea点击:

Method Syntax in Objective-C

objective-csyntaxmethods

提问by Craig

Can someone explain this method declaration syntax for me? In this function, the number of rows of a UIPickerView (slot machine UI on the iPhone) is being returned. From my understanding, the Method is called 'pickerView', and returns an NSInteger.

有人可以为我解释这个方法声明语法吗?在这个函数中,返回的是一个 UIPickerView(iPhone 上的老虎机 UI)的行数。根据我的理解,该方法称为“ pickerView”,并返回一个 NSInteger。

It passes in a pointer to the UIPickerview called 'pickerView' ... first, why is the method called the same name as the parameter?

它传入一个指向 UIPickerview 的指针,名为 ' pickerView' ... 首先,为什么该方法与参数名称相同?

Next there is NSInteger parameter called component that tells us which component we are counting the rows for. The logic to decide which is in the body of the method.

接下来是名为 component 的 NSInteger 参数,它告诉我们要为哪个组件计算行数。决定哪个在方法体中的逻辑。

What is 'numberOfRowsInComponent? It seems to describe the value we are returning, but it is in the middle of the parameters.

什么是' numberOfRowsInComponent?它似乎描述了我们返回的值,但它位于参数的中间。

- (NSInteger) pickerView:(UIPickerView *)pickerView 
 numberOfRowsInComponent:(NSInteger)component
{
    if (component == kStateComponent)
        return [self.states count];

    return[self.zips count];
}

回答by locriani

Objective-C methods are designed to be self documenting, and they borrow from the rich tradition of Smalltalk.

Objective-C 方法被设计为自文档化,它们借鉴了 Smalltalk 的丰富传统。

I'll try to explain what you have here, -(NSInteger) pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component.

我会试着解释你在这里有什么,-(NSInteger) pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component

  • - (NSInteger)
    This first portion indicates that this is an Objective C instancemethod that returns a NSInteger object. the -(dash) indicates that this is an instancemethod, where a +would indicate that this is a classmethod. The first value in parenthesis is the return type of the method.

  • pickerView:
    This portion is a part of the message name. The full message namein this case is pickerView:numberOfRowsInComponent:. The Objective-C runtime takes this method information and sends it to the indicated receiver. In pure C, this would look like
    NSInteger pickerView(UIPickerView* pickerView, NSInteger component). However, since this is Objective-C, additional information is packed into the message name.

  • (UIPickerView*)pickerView
    This portion is part of the input. The input here is of type UIPickerView*and has a local variable name of pickerView.

  • numberOfRowsInComponent:
    This portion is the second part of the message name. As you can see here, message names are split up to help indicate what information you are passing to the receiver. Thus, if I were to message an object myObject with the variables foo and bar, I would type:
    [myObject pickerView:foo numberOfRowsInComponent:bar];
    as opposed to C++ style:
    myObject.pickerView(foo, bar);.

  • (NSInteger)component
    This is the last portion of the input. the input here is of type NSIntegerand has a local variable name of component.

  • - (NSInteger)
    第一部分表明这是一个返回 NSInteger 对象的 Objective C实例方法。的-(短划线)表示这是一个实例的方法,其中+将指示这是一种方法。括号中的第一个值是方法的返回类型。

  • pickerView:
    这部分是消息名称的一部分。在这种情况下,完整的消息名称pickerView:numberOfRowsInComponent:. Objective-C 运行时获取此方法信息并将其发送到指定的接收器。在纯 C 中,这看起来像
    NSInteger pickerView(UIPickerView* pickerView, NSInteger component). 然而,由于这是 Objective-C,附加信息被打包到消息名称中。

  • (UIPickerView*)pickerView
    这部分是输入的一部分。此处输入的类型UIPickerView*为pickerView 的局部变量名称。

  • numberOfRowsInComponent:
    这部分是消息名称的第二部分。正如您在此处看到的,消息名称被拆分以帮助指示您将哪些信息传递给接收者。因此,如果我要使用变量 foo 和 bar 向对象 myObject 发送消息,我会输入:
    [myObject pickerView:foo numberOfRowsInComponent:bar];
    而不是 C++ 风格:
    myObject.pickerView(foo, bar);

  • (NSInteger)component
    这是输入的最后一部分。这里的输入是类型NSInteger并且具有组件的局部变量名称。

回答by e.James

In Objective-C, the name of a method is composed of all of the portions of the declaration that are not arguments and types. This method's name would therefore be:

在 Objective-C 中,方法的名称由声明中所有不是参数和类型的部分组成。因此,此方法的名称将是:

pickerView:numberOfRowsInComponent:

The method would be equivalent to a C-style function that looked as follows:

该方法相当于一个 C 风格的函数,如下所示:

edit:(with thanks to Jarret Hardie):

编辑:(感谢Jarret Hardie):

NSInteger pickerViewNumberOfRowsInComponent(UIPickerView * pickerView, NSInteger component)

回答by Karolis

Adding to the previous answers, I'd just like to say that Objective-C methods (or messages if you prefer) have externaland internalparameter names.

添加到前面的答案中,我只想说 Objective-C 方法(或消息,如果您愿意)具有外部内部参数名称。

So in this case:

所以在这种情况下:

- (NSInteger) pickerView:(UIPickerView *)pickerView 
 numberOfRowsInComponent:(NSInteger)component

numberOfRowsInComponentis the externalname, the one that you would use when calling this method from the outside.

numberOfRowsInComponent外部名称,从外部调用此方法时将使用的名称。

And componentis the internalname of the parameter, the one you use to refer to the parameter from inside of the method.

andcomponent是参数的内部名称,用于从方法内部引用参数的名称。

Hope that clears it up a bit.

希望能稍微澄清一下。

回答by wilczarz

It seems to me that Objective-C method signatures are more like sentences. Each parameter deserves a part in method's name. For instance, in C we could have a method (setPersonData) for setting some information about person:

在我看来,Objective-C 方法签名更像是句子。每个参数都应该在方法名称中占有一席之地。例如,在 C 中,我们可以有一个方法 ( setPersonData) 来设置有关人员的一些信息:

void setPersonData( char* name, int age, float height ) {

and in Objective-C the method would be more descriptive (setPersonName:andAge:andHeight:), like

而在 Objective-C 中,该方法将更具描述性(setPersonName:andAge:andHeight:),例如

- (void) setPersonName: (char *)name andAge:(int)age andHeight:(float)height {