3

Data encapsulation, or as I like to call it, Who owns it and who needs to know about it, makes up a lot of object-oriented programming. The who needs to know is often satisfied by accessor methods, but these get to be pretty expensive if they all result in an objc_msgsend just to read a variable. C++ answers the problem with inline methods - use the "inline" keyword before the definition, or define the method within the class declaration, and the compiler puts the accessor code within the caller's code, saving the overhead associated with an actual function call.

class IntWrapper {
public:
   int getInt() { return anInt; }
protected:
   int anInt;
};

Similar syntax is rewarded by a complier error in Objective-C. Having searched the language guides in Xcode ("[Object-Oriented] Programming with Objective-C"), I don't see any relevant reference to "inline" of a method. Is there such thing as inline in Objective-C? Is it called something else? If anyone could point me to the documentation that references inline, much appreciated.

Using the simple test code:

@interface ClassA : NSObject
{
   int anInt;
}
- (int) anInt;
@end

@implementation ClassA
- (int) anInt { return anInt; }
@end

and looking at the assembly of the code that uses it, it looks like about 25 instructions.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Only C functions can be inlined, Objective-C methods unfortunately not. – Cristik Jan 22 '16 at 02:13
  • You can find out more about this here: http://stackoverflow.com/questions/8194504/does-llvm-convert-objective-c-methods-to-inline-functions – Cristik Jan 22 '16 at 02:16
  • 1
    And it's worth adding that the convention is to use `@property` to avoid the boilerplate of declaring variables and it's accessors. – Raphael Oliveira Jan 22 '16 at 02:19

1 Answers1

3

All Objective-C methods are dispatched dynamically. They can be overridden by subclasses. They can even be replaced at runtime ("swizzled") by the Objective-C runtime API.

In some ways, they are similar to virtual methods in C++.

As such they can't be inlined.

By the way, the technique you cite violates the principle you cite ("Who owns it and who needs to know about it?"). Putting the implementation in the class declaration exposes implementation detail to clients who don't need to know it. Furthermore, the compiler inlining the code into clients prevents that implementation from changing without a recompile, which is the fragile base class problem. Modern Objective-C avoids the fragile base class problem, which means a framework class can change what instance variables it has without breaking clients.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • Also, key value coding – Kevin Jan 22 '16 at 02:59
  • 1
    I think C++ programmers necessarily take the view that, if you're reading the interface then you're reading only the method and variable declarations, in the `public:` section — not the definitions or anything in `private:` (and whether `protected:` is relevant depends on the class you're authoring, obviously). I'm fairly sure I'm not out of date to say that even private storage must be published. It's the opposite of the Objective-C mindset where we can't have any methods that are genuinely private and instead simply don't advertise them. – Tommy Jan 22 '16 at 03:08
  • 1
    ... rather like Swift, I guess. Though Xcode won't automatically show you only the interface part. Presumably other editors will. I think I'm making a very minor distinction though. Naturally I've already given you a positive vote. – Tommy Jan 22 '16 at 03:29
  • @Tommy, true that C++ requires instance variables to be declared in the class declaration. You can use [pImpl](http://c2.com/cgi/wiki?PimplIdiom) to mitigate it a bit. – Ken Thomases Jan 22 '16 at 03:33