0

Is it possible to use the return value of a method as the argument for a different method invocation? I'm using Objective-C.

What I'm going for is something like this:

stringOutput = [object1 method1:[object2 method2:[object3 method3]]]; 

where method 3 returns a string that goes into method 2, which returns a string that goes into method 1, which returns a string that goes into stringOutput.

7
  • It doesn't make sense to me; it seems like you're not meaning 'argument' in a standard sort of way. Can you explain some more? Commented Jun 28, 2011 at 15:16
  • 1
    Short answer, no. An argument by definition, does not have arguments. You can chain methods though - something like [obj method: [myargissuppliedby anotherMethodArg]]; Commented Jun 28, 2011 at 15:17
  • I believe I meant parameter, not argument. I edited above. Commented Jun 28, 2011 at 15:20
  • Are you referring to a feature available in a different language? If so, what is it? Commented Jun 28, 2011 at 15:25
  • @Deepak: No, not that I'm sure of. Basically I want to do this: stringOutput = [class method:[class method:[class method]]]; Commented Jun 28, 2011 at 15:27

1 Answer 1

4

Do you mean sending the result from one method as the parameter for another?

NSString *string = [self myMethod:[self myMethod2]]; 

Where the methods are

- (NSString *)myMethod2 { return @"A String"; } - (NSString *)myMethod:(NSString *)string { // Do something with string // Return another string return @"Something else"; } 
Sign up to request clarification or add additional context in comments.

4 Comments

This! Parameter was the word I was looking for, not argument. But could you embed that again? As in -(NSString *)myMethod:[NSString(myMethod2):NSString(myMethodOfMyMethod2)]
You're not making much sense - what is the syntax you're aiming for in this comment?
@James - The words "parameter" and "argument" are synonymous in this context.
@James, @Sherm - "parameter" and "argument" are never synonymous. Methods have parameters; when invoking a method, the caller must provide an argument for each parameter of that method. Nick's answer is correct - it's a fundamental part of the language. You're doing it all the time already probably - [[MyClass alloc] init] is probably the most common example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.