sarmenhbbbb
sarmenhbbbb

Reputation: 29

What does it mean to return an object in a method?

I still cannot understand what does it mean to return an object in a method. What would its value mean?

If I have something like this:

-(ClassName *) methodName: (int) arg {

return arg;

}

I can't understand how an object can be returned through a method as the above. If someone can help me understand.

Thanks.

Upvotes: 1

Views: 1243

Answers (3)

Chuck
Chuck

Reputation: 237040

You would return an object by returning an object. For example, you could ignore the argument:

- (ClassName *)methodName:(int)arg {
    return [[[ClassName alloc] init] autorelease];
}

You could turn the int into an object:

- (NSNumber *)methodName:(int)arg {
    return [NSNumber numberWithInt:arg];
}

You could use the argument in some calculation to determine some property of the object returned. You could process the argument and return an object indicating the status of the calculation. And so on and so on. There's a practically unlimited range of ways you could return an object from a method. All it requires is that some object be created or accessed and then returned.

Upvotes: 2

Tom Dalling
Tom Dalling

Reputation: 24125

Think of methods like they are functions in math. In math, sin(180) is equal to 0. sin is the method, 180 is the argument and 0 is the return value of the method. An example of sin in objective-c might go like this:

-(double) sin:(double)angleInDegrees;
{
    double sinValue;
    //calculate the return value here and store it in sinValue.
    //for example, if angleInDegrees is 180, then set sinValue to 0
    return sinValue;
}

Returning objects is exactly the same. Look at this example:

-(NSString*) sayHelloTo:(NSString*)name;
{
    return [NSString stringWithFormat:@"Hello %@!", name];
}

If I were to write it like a math function, then sayHelloTo(@"Tom") is equal to @"Hello Tom!". The only difference is that @"Hello Tom!" is an NSString object, not a double.

Upvotes: 0

stefanB
stefanB

Reputation: 79790

The above method returns a pointer to arg which is of type ClassName*.

I assume explaining the question would assume basic knowledge of how functions are called, how passed values are pushed on stack before function call and how return values is returned from a function.

In this specific case your arg variable is part of a class, meaning that it is stored in memory that is part of the object. When you return pointer to it you are pointing to a specific area of memory within the object.

Another option is to return copy of the value. It would mean make a copy and return it.

The difference is that if you return pointer to objects internal variable that object state could be modified from outside.

If you return copy that copy can be modified and the original object will not change.

Not sure if that helps, but you are asking about very basic software development topic which assumes some background knowledge.

Maybe specify what exactly you are looking for?

Upvotes: 1

Related Questions