Reputation: 29
I'm new to iOS and I'm trying to figure how to change part of the code to have 2 returns instead of one (if possible and int and a double). I understand what the code does but I don't know how to implement multiple returns?
-(int)findmaxpos:(double*)array l:(int)length s:(int)start e:(int)end{
// Find array ending point to avoid exception
int new_end = MIN(end,length-1);
// Find array mean
double mean = 0;
for (int i = start; i<= new_end; i++){
mean += array[i];
}
mean = mean / (new_end-start+1);
// Calculate variance
double var = 0;
for (int i = start; i<= new_end; i++){
var += pow((array[i] - mean),2);
}
var = var / (new_end-start+1);
double sd = sqrt(var);
// Calculate limit add 2.5* standart deviation to get approximatelly 95% of the values
double limit = mean + (2.5 * sd);
// Find first position above limit
int pos = -1;
while (pos<0) {
for (int i=start; i<=new_end; i++) {
if (array[i]>=limit){
pos = i;
break;
}
}
limit = limit * 0.9;
}
double max =array[pos];
// Check neibouring 10 positions to find a value greater than current.
for (int i=MAX(pos-5,start); i<=MIN(new_end,pos+5); i++) {
if (array[i]>=max){
max = array[i];
pos = i;
}
}
// Return value
//cast max as double??
return pos; //20/02 max inclusion
}
Upvotes: 0
Views: 97
Reputation: 5762
It is true that objective C does not allow returning multiple values. You could use an NSArray to return multiple values, but i personally prefer NSDictionary. For eg.
- (NSDictionary*)doSomeTaskAndReturnMultipleValues {
//Do some task let's say you have some objects to return named a,b,c
return @{@"key1":a, @"key2":b, @"key3":c};
}
This allows you to return values based on key value pairs rather than depending on the order of the values returned which can easily go wrong.
If you still want you could use an array, if you know you want the returned values in a particular order.
- (NSArray*)doSomeTaskAndReturnMultipleValues {
//Do some task let's say you have some objects to return named a,b,c
return @[a,b,c];
}
Hope this helps.
Upvotes: 1
Reputation: 11211
Objective-C does not allow multiple return values. The standard way to do this would be to wrap your two return values in an array, like NSArray
, or to create a custom struct that you will return, which has the matching members.
Upvotes: 0