Objective-C中的方法语法 [英] Method Syntax in Objective-C

查看:147
本文介绍了Objective-C中的方法语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以为我解释此方法声明语法吗?在此功能中,将返回UIPickerView(iPhone上的老虎机UI)的行数.据我了解,该方法称为"pickerView",并返回一个NSInteger.

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.

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

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

接下来,有一个称为组件的NSInteger参数,它告诉我们我们要为哪个组件计数的行.决定哪个在方法主体中的逻辑.

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.

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

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

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

    return[self.zips count];
}

推荐答案

Objective-C方法旨在自我记录,并借鉴了Smalltalk的丰富传统.

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

我将尝试在这里解释您的内容,-(NSInteger) pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component.

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

  • - (NSInteger)
    第一部分表明这是一个返回NSInteger对象的Objective C 实例方法. -(破折号)表示这是 instance 方法,其中+表示这是 class 方法.括号中的第一个值是方法的返回类型.

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

pickerView:
这部分是消息名称的一部分.在这种情况下,完整消息名称pickerView:numberOfRowsInComponent:. Objective-C运行时将使用此方法信息,并将其发送到指定的接收器.在纯C语言中,这看起来像
NSInteger pickerView(UIPickerView* pickerView, NSInteger component).但是,由于这是Objective-C,因此其他信息将打包到消息名称中.

pickerView:
This portion is a part of the message name. The full message name in 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
这部分是输入的一部分.此处的输入为UIPickerView*类型,其局部变量名称为pickerView.

(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:
这部分是消息名称的第二部分.如您在此处看到的,消息名称被拆分以帮助指示您要传递给接收者的信息.因此,如果我要向对象myObject传递变量foo和bar,我将输入:
[myObject pickerView:foo numberOfRowsInComponent:bar];
与C ++风格相反:
myObject.pickerView(foo, bar);.

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
这是输入的最后一部分.这里的输入为NSInteger类型,其局部变量名称为component.

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

这篇关于Objective-C中的方法语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆