简体   繁体   English

基本Obj-C问题回合方法

[英]Elementary Obj-C Question bout Methods

take example: 举个例子:

-(void)setName:(NSString *)name age:(int)age;

How would you call this method (in other words, the method's name is setName but what is the "age" parameter doing in there) and what do the types in parentheses mean? 您将如何称呼此方法(换句话说,该方法的名称为setName,但其中的“ age”参数在做什么)以及括号中的类型是什么意思? Is it just a way to tell the compiler what types are being returned? 这只是告诉编译器返回什么类型的一种方法吗?

[ myObject setName: @"Adam" age:18 ];

The age parameter is the second parameter in the method signature. age参数是方法签名中的第二个参数。

The types in parentheses are the expected types for the argument. 括号中的类型是该参数的预期类型。 eg name is expecting only an NSString and age is expecting only an int . 例如, name只期望一个NSStringage只期望一个int

The - means that the method is an instance method, not a class method, which is denoted using a + instead. -表示该方法是实例方法,而不是类方法,而是使用+表示。

The type in parentheses right after the - is the return type. 括号后面的-是返回类型。

This is a great site for learning the basics of Objective-C: CocoaDevCentral 这是学习Objective-C基础知识的好网站: CocoaDevCentral

This is the standard Objective-C method syntax. 这是标准的Objective-C方法语法。 This could be read as: 可以理解为:

A method with no return value (void) that sets the name of the object (an NSString * parameter) and the age (and integer parameter). 一种没有返回值(无效)的方法,该方法设置对象的名称(NSString *参数)和年龄(和整数参数)。

Dissecting the method: 剖析方法:

  • "-" The hyphen states that this is an instance method. “-”连字符表示这是一个实例方法。

  • (void) The return type is void - or no return type expected (无效)返回类型为void-或没有预期的返回类型

  • setName:(NSString *) The first parameter to be passed is the "name" and is an NSString *. setName:(NSString *)要传递的第一个参数是“名称”,并且是NSString *。

  • age:(int)age The second parameter to be passed is the "age" and is an int. age:(int)age要传递的第二个参数是“ age”,它是一个整数。

In reality, the method syntax is actually quite self-documenting once understood (and quite foreign if you're used to more tradition C/C++ or Java syntax). 实际上,方法语法一旦被理解,实际上是非常自我记录的(如果您习惯了更传统的C / C ++或Java语法,则是非常陌生的)。

The actual example of the call of this method would be: 该方法的实际调用示例为:

[someObject setName:@"Rich" age:101];

To answer, one would need a bit more information, but I'll be guessing this is from some sort of class named aClass, and you have an instance of aClass, named instance. 要回答这个问题,还需要更多信息,但是我猜这是来自某种名为aClass的类,并且您有一个aClass实例,名为instance。

-(void)setName:(NSString *)name age:(int)age;

means you have a method, named setName:age:, that needs two arguments, one NSString, one int, and it returns a void. 表示您有一个名为setName:age:的方法,该方法需要两个参数,一个为NSString,一个为int,并返回一个void。 As it has a - as it's first character, it is an instance method. 作为第一个字符,它是一个实例方法。

[instance setName:@"James Hargrove" age:21];

Would call setName:age: on the instance. 将在实例上调用setName:age:。

(The instance should be created using, say, (实例应使用

aClass *instance = [[aClass alloc] init];

which would create an instance of aClass named instance, and initialize it. 这将创建一个名为instance的aClass实例,并对其进行初始化。

The method name is actually this: 方法名称实际上是这样的:

setName:age:

You call it like this: 您这样称呼它:

[someObject setName:@"Alice" age:20];

setName:age: is also the unique signature of that method, and with that signature you can call that method on any object you wish. setName:age:也是该方法的唯一签名,使用该签名,您可以在所需的任何对象上调用该方法。 For example: 例如:

NSArray* objects = ...

SEL mySelector = @selector(setName:age:);
for (id object in objects)
{
    if ([object respondsToSelector:mySelector])
    {
        [object setName:@"Alice" age:20];
    }
}

what do the types in parentheses mean? 括号中的类型是什么意思? Is it just a way to tell the compiler what types are being returned? 这只是告诉编译器返回什么类型的一种方法吗?

Yes, those are "C casts". 是的,这些是“ C强制转换”。 If everything was an object you wouldn't need those, but because you can pass and return plain old C types to and from your methods, the compiler needs to know the types of your parameters and return values. 如果一切都是对象,则不需要这些对象,但是由于您可以在方法中传递普通的旧C类型并从中返回,因此编译器需要知道参数的类型和返回值。

You'd call this method like so: 您将这样调用此方法:

[classInstance setName:@"name" age:123];

The first instance of "age:" indicates that the method receives another parameter, called "age" when used in the implementation of the method. “ age:”的第一个实例指示该方法在实现该方法时使用另一个参数,称为“ age”。

The types in parentheses indicate the types of data that are expected for each parameter, with the exception of the first one, "void", which means that this method returns nothing. 括号中的类型表示每个参数所需的数据类型,第一个参数“ void”除外,这意味着此方法不返回任何内容。

So, you would call this method as follows. 因此,您可以按以下方式调用此方法。

Say it is a method of an object named foo (of class Foo). 说这是一个名为foo(属于Foo的类)的对象的方法。 Then you would call: 然后,您将调用:

[foo setName:someName age:someAge].

If it were a static method, it would be preceded by a + instead of a minus as follows: 如果它是静态方法,则在其前面加上+而不是减号,如下所示:

+(void)setName:(NSString *)name age:(int)age;

Then you would call 那你会打电话

[Foo setName:someName age:someAge] //use the classname instead of the object name

The types are indeed there for type-checking by the compiler. 这些类型确实存在,供编译器进行类型检查。 You'll get warnings if you pass the wrong sort of data, and you will get warnings if your header doesn't match your implementation. 如果传递的数据类型错误,则会收到警告,如果标头与实现不匹配,则会收到警告。

You can actually write Obj-C functions in a couple of different styles though, omitting some of this stuff. 您实际上可以用几种不同的样式编写Obj-C函数,而忽略其中的一些内容。 You can even write straight up C-style. 您甚至可以直接编写C样式。

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

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