1

This is a parameterless function:

- (void) doSomething;

This is a hypothetically function with "parameterless parameter":

- (void) convertCelcius:(CGFloat)celcius toFarenheit;

So I can call it like this:

CGFloat result = [myClass convertCelcius:50.5 toFarenheit];
CGFloat result = [myClass convertCelcius:50.5 toKelvin];
CGFloat result = [myClass convertKelvin:352 toCelcius];

Instead of:

CGFloat result = [myClass farenheitFromCelcius:50.5];
CGFloat result = [myClass kelvinFromCelcius:50.5];
CGFloat result = [myClass celciusFromKelvin:352];

Is this possible? Thanks.

Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124
  • @p4sh4 maybe it's a duplicate, but the answer shows something I never thought of before, and didn't present at the duplicates. – Chen Li Yong Dec 15 '16 at 04:03

1 Answers1

3

No, Objective-C syntax doesn't allow that. You have to pass something there if you have a second keyword. Brad Cox, the creator of Objective-C, explains why here.

Anyway, in Cocoa, if a message is mainly intended to be used for its return value, rather than for side effects, we usually try to name the function after its return value—a noun phrase rather than a verb phrase. So a name like fahrenheitFromCelcius: is better anyway.

On the other hand, you could follow the example of -[UIView convertPoint:toView:]. I've never understood why the UIKit designers named it this way. Anyway, that would lead to something like:

typedef enum {
    TSKelvin,
    TSCelsius,
    TSFahrenheit
} TemperatureScale;

CGFloat result = [myClass convertDegrees:50.5 from:TSCelsius to:TSFahrenheit];

Note also that the correct spellings are “Fahrenheit” and “Celsius”.

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848