开发者

Elementary Obj-C Question bout Methods

开发者 https://www.devze.com 2022-12-15 19:21 出处:网络
take example: -(void)setName:(NSString *)name age:(int)age; How would you call this method (in other word开发者_JAVA技巧s, the method\'s name is setName but what is the \"age\" parameter doing in t

take example:

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

How would you call this method (in other word开发者_JAVA技巧s, the method's name is setName but what is the "age" parameter doing in there) and what do the types in parentheses mean? 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.

The types in parentheses are the expected types for the argument. e.g. name is expecting only an NSString and age is expecting only an 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


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.

-(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. 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.

(The instance should be created using, say,

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

which would create an instance of aClass named instance, and initialize it.


This is the standard Objective-C method syntax. 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).

Dissecting the method:

  • "-" The hyphen states that this is an instance method.

  • (void) The return type is void - or no return type expected

  • setName:(NSString *) The first parameter to be passed is the "name" and is an NSString *.

  • age:(int)age The second parameter to be passed is the "age" and is an int.

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).

The actual example of the call of this method would be:

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


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. 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". 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.


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.

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.


So, you would call this method as follows.

Say it is a method of an object named foo (of class 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. You can even write straight up C-style.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号