0

I am trying to declare a method that has multiple input and output parameters. I have no problems with multiple input parameters declaration like this:

     - (float)tCorrection:(float)t2 tCableBase:(float)t1 CableMaterial: (NSString*)CopperOrAl;

In this case we have 3 input parameters (different types) and one float return type.

My problem is how to declare a function that has more that 1 return parameters. I tried different syntax's and no luck.

Any help is appreciated.

3
  • What are the values you need to return ? Commented Jun 21, 2013 at 13:38
  • Why don't you use NSDictionary or NSMutableArray ? Commented Jun 21, 2013 at 13:38
  • I agree with lxt, what you need is a dictionary Commented Jun 21, 2013 at 13:39

3 Answers 3

1

First, your method name is really sub-optimal. Instead of:

     - (float)tCorrection:(float)t2 tCableBase:(float)t1 CableMaterial: (NSString*)CopperOrAl;

I would suggest something like:

 - (float)applyCorrection:(float)correction toCableBase:(float)cableBase withCableMaterial:(CableMaterial)material;

Where you define CableMaterial to be an enumerated type of materials.

Next, to answer your question, you could use return-by-reference as duDE suggests.

But, don't. Smells like bad design.

Instead, it really sounds like you need a class whose instances can be configured with various parameters and then queried to get the calculated results. This will produce a much cleaner application design.

I would imagine something like a Cable class that has properties like material, base, correction, etc... and then various readonly properties that calculate the various needed values.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks bbum for your comment. It shows me another approach.
0

The recommended way of returning multiple values is:

  • wrap it up and return a struct
  • wrap it up and return a class
  • return a dictionary/array

Using a pass-by-reference approach should generally not be used and is used mostly for NSError**.

Comments

0

Generally There is no any Syntax of Method That Return Multiple Values But If your Have Multiple Values and your want to return it then create NSMutableArray or NSMutableDictionary That useful for return multiple values.

Such Like,

- (NSMutableArray *)tCorrection:(float)t2 tCableBase:(float)t1 CableMaterial: (NSString*)CopperOrAl;
{
     NSMutableArray *temArray = [[NSMutableArray alloc] init];
     [temArray addObject:[NSString stringWithFormat:@"%f", t2]];
     [temArray addObject:[NSString stringWithFormat:@"%f", t1]];
     [temArray addObject:[NSString stringWithFormat:@"%@", CopperOrAl]];

   return temArray;
}

And For Got Value From Return Array

NSMutableArray *returnArray = [self tCorrection:2.5 tCableBase:23.5 CableMaterial:@"MyString"];

NSLog(@"%@", returnArray);

2 Comments

I would only do it this way if the number or type of return parameters are unknown at compile time. You'd miss out on compile-time type checking and Objective-C's explicit style and syntax. See duDE's answer for the preferred method.
Indeed, this approach is rather error prone and you lose type safety, named parameters, clarity and more.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.