tags:

views:

60

answers:

3

i still cannot understand what it means 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 cant understand how an object can be returned through a method as the above. if someone can help me understand. thanks.

+1  A: 

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?

stefanB
im just trying to understand what this would mean : ClassName * i usually put int or void in the paranthesis to return an int value or return nothing when a void is in there. from what i remember * is setting up a pointer. so it has to do something with member right?
sarmenhbbbb
it means pointer, so you are returning pointer to the member variable, you are returning a memory address
stefanB
A: 

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.

Tom Dalling
+2  A: 

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.

Chuck