views:

84

answers:

2

Hi all,

I am an Objective C noob and need a bit of help.

I need to pass a function 2 integers A and B.

The called function then checks if A > B, A = B or A < B and passes back a string.

If A > B then it must pass back "HOT"

If A = B then it must pass back "MEDIUM"

If A < B then it must pass back "COLD"

Also how do I call this function from within another function?

Any help would be appreciated.

Thanks.

+1  A: 
-(NSString*) myMethod: (int) one two:(int)two {
     if( one > two ) return @"HOT";
     if( one == two ) return @"MEDIUM";
     return @"COLD";
}

You can then call this like such:

[myObject myMethod:10 two:30]; //returns "COLD"
Jacob Relkin
+5  A: 
- (NSString *)stringForTemperature:(int)temperature base:(int)base {
    if (temperature > base) {
        return @"HOT";
    } else if (temperature < base) {
        return @"COLD";
    } else {
        return @"MEDIUM";
    }
}

- (void) otherFunction {
    NSString *temperatureString = [self getStringForTemperature:A base:B];
}
Ed Marty
Change that method name to `stringForTemperature:base:` to follow the code convention and you'll get my +1. :P
bddckr
Coding convention shmoding shm... shmonv.... whatever I'll just do it :)
Ed Marty
Well the "get" in front is only used for methods that change stuff by reference. You'll get your +1 regardless. Especially for a new dev it is important to understand why stuff is named like that. Having these clear conventions make it easier to work with all the APIs out there.
bddckr
Brilliant thanks guys! working perfectly.
Dalan