3

I am learning Objective-C and iOS development. I think I have a handle on method syntax. Method Syntax in Objective C

If I am understanding correctly, this instance method called reset should return an IBAction object. But it just seems to be setting instance variables (which are UI text fields with those names). Why doesn't it have a return statement returning an IBAction object?

- (IBAction)reset {               //why does it not have a return statement?
    fahrenheit.text = @"32";
    celsius.text = @"0";
    kelvin.text = @"-273.15";
}

I am used to .NET code that would look like this (putting it in pseudocode for non-NET folks):

public function reset () returns IBAction
        me.fahrenheit.text = "32";
        me.celsius.text = "0"
        me.kelvin.text = "-273.15"
        return new IBAction    //I would expect something like this in obj C, based on my experience with .NET languages
end function
Cœur
  • 37,241
  • 25
  • 195
  • 267
bernie2436
  • 22,841
  • 49
  • 151
  • 244
  • For reference, `-(ClassType)method` pretty much never happens. Pass-by-value only works for primitives and structs, IIRC; for classes, you have to pass them around (and return them) by pointer. Even your object variables are pointers. (Well, handles, but still.) – cHao Oct 14 '12 at 15:20
  • Which means if you see `-(SomeType)method`, `SomeType` is probably not a class. – cHao Oct 14 '12 at 15:33

1 Answers1

8

IBAction is typedef'd void. It is used by Xcode as a hint that the method is to be available for "hookup" in interface builder.

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • @ctrahey--so I can interpret this method as "returning void"/ie not returning an object (or a reference to an object, for objective C). Because the method returns void, the compiler is not expecting it to return a reference and so allows no return statement. Got it? – bernie2436 Oct 14 '12 at 15:37
  • Perfect, yes! It is allowable to have `return;`, but also perfectly legal to not have a return statement. – Chris Trahey Oct 14 '12 at 15:38
  • 1
    @akh2103 Incidentally you can see the `#define` (not typedef) for `IBAction` in `UINibDeclarations.h`. – rob mayoff Oct 30 '12 at 03:13