Instance Variables Specified in ObjC Implementation File

I watched the WWDC ARC entry video and I saw something that I had never seen in ObjC before one of Apple's engineers talked about the Stack example.

The following code was used for an example stack with ARC:

@implementation Stack { // instance variable declared in implementation context NSMutableArray *_array; } - (id)init { if (self = [super init]) _array = [NSMutableArray array]; return self; } - (void)push:(id)x { [_array addObject:x]; } - (id)pop { id x = [_array lastObject]; [_array removeLastObject]; return x; } @end 

Note the instance variable declared immediately after the @implementation directive.

Now, what surprised me was that the instance variable could indeed be declared in the implementation file, without a static variable. My questions will be as follows:

  • Is this some kind of new design introduced in the iOS 5 SDK, or is it possible for a long time?
  • Would it be good practice to declare instance variables in an implementation if instance variables should not be accessible outside the object? This seems to be cleaner than using the @private directive.
+47
objective-c automatic-ref-counting
Jul 22 2018-11-11T00:
source share
3 answers

This is a really new language feature, and if you have to declare your ivars (and not just declare properties and let the compiler generate ivars for you), this is good practice. Your header files in theory should expose a public interface for your classes; everything else belongs to the implementation.

One caveat is that ivars file implementations are not visible to subclasses, which can sometimes be a little awkward if you have manually created setters and getters that you need for the subclass.

+39
Jul 31 '11 at 18:12
source share

Declaring iVars inside an implementation is, of course, a new construct in object C. You must use xcode4.2 and have the LLVM compiler selected in the build settings. The idea is to clear the header files. You can list your ivars inside braces like this example;

 @implementation MyClass { int var1; int var2; } 

The answer given by Rahul is not entirely correct, although you can share the variables as he says that their compiler will be considered as static. Probably, for the cases when he used them, it did not matter.

+17
Aug 03 2018-11-11T00:
source share

I am new to Objective-C, and I found that the practice of declaring ivars in the header is very strange. This means declaring the internal state of an object in its public heading, which contradicts the concept of encapsulation.

For example, you have an iPad. Apple does not want you to violate the iPad, open and peep, and tinker with the items inside. If they want you to change something, the iPad will have a setting that allows you to change it.

Similarly, I do not want other programmers to see ivars of my objects. His inner state is my object. If I want you to achieve an inner state, I will declare properties for it.

So, as in other languages, I would hide my ivars inside the implementation file and not declare them in the header.

The ivars ad in the title just amazes me very weirdly. These ivars are implementation specific and should not just be part of the header file.

+4
09 Oct '12 at 9:05
source share



All Articles