0

Here's a (reduced) class declaration from an example on apple's developer:

@interface myController : UITableViewController {

    NSArray *samples;
}

@property (nonatomic, retain) NSArray *samples

What is the purpose of declaring

{

    NSArray *samples;
}

when you declare it again as a property? If you leave out:

{

    NSArray *samples;
}

you can still use @synthesize in your .m and get a reference to it!

I'm a little confused as to the purpose of the first declaration.

Thanks

Max
  • 16,679
  • 4
  • 44
  • 57
IIS7 Rewrite
  • 779
  • 3
  • 16
  • 31

2 Answers2

3

Properties are just a handy way to declare accessors to you data. It usually leads to some member variable but not necessarily. And that member var can have different name:

@interface myController : UITableViewController {

    NSArray *mSamples;
}

@property (nonatomic, retain) NSArray *samples
@end

@implementation
@synthesize samples = mSamples;
@end

Or you can use properties without vars at all:

@interface myController : UITableViewController {

}

@property (nonatomic, retain) NSArray *samples
@end

@implementation
   -(NSArray*) samples {
    //you can for example read some array from file and return it
   }

   -(void) setSamples:(NSArray*) arr {
     //write that array to file or whatever you want
    }
@end

With new compiler you can use properties without ivars at all, compiler will generate them for you implicitly.

Max
  • 16,679
  • 4
  • 44
  • 57
  • Ok, I think I understand it now. The confusing part for me was "accessors to your data", but you could also leave the "data" part out! – IIS7 Rewrite Dec 07 '12 at 06:11
  • I think the example of "properties without ivars" is not quite correct. Unless you make it `@dynamic`, there is an ivar. In recent Objective-C, unless otherwise specified (as you show in your first example) the ivar has the same name as the property but with an underscore prefix — in this case, `_samples`. If you want a property without a backing ivar (by providing getter/setter methods as you show), say `@dynamic samples;` in the @implementation. – Jon Reid Dec 07 '12 at 06:26
  • @JonReid I mean't when you're defining your own accessor methods – Max Dec 07 '12 at 06:27
  • @Max, I stand corrected: I just took a look using the debugger. You're right, if you provide both a getter and a setter, the compiler sees this and doesn't create a backing instance variable. – Jon Reid Dec 07 '12 at 06:46
1

With a property declaration, there is no purpose or benefit in explicitly declaring the backing instance variable. It's just leftovers from habit.

Edit: For iOS or Mac 64-bit Intel, explicitly declaring ivars was never needed for properties. But they were needed for other Mac work — hence the examples.

Also, I did find a difference. When an ivar is explicitly declared, unless you state otherwise, it is a protected ivar, available to subclasses. But when an ivar is implicitly created for a property, subclasses don't have access to the ivar.

Jon Reid
  • 20,545
  • 2
  • 64
  • 95
  • Further on this: initially you couldn't declare `@property`s and `@synthesize`s but had rather to write every getter and setter yourself with an explicit instance variable. From there we've transitioned in small steps to being able just to declare the `@property` with no `@synthesize` or instance variable. Older code and those people used to older patterns will often do more than the minimum required by the latest tools through force of habit. – Tommy Dec 07 '12 at 06:08
  • well, in traditional object oriented languages, getters and setters are used on accessors to private variables. So you're saying that in Objective-C, as long as you declare a property you don't really need a variable that the property accesses. The property actually serves as the public getter and setter, whether or not there's a private variable linked to it, right? – IIS7 Rewrite Dec 07 '12 at 06:12
  • @Tommy All the original examples showed the declarations of ivars. But properties were added in Objective-C 2.0. For iOS at least, ivar declarations were immediately unnecessary, but I think it wasn't the case for MacOS (hence the examples). But the default name of the backing ivar has changed recently, from being the same as the property to having a leading underscore. – Jon Reid Dec 07 '12 at 06:20
  • @IIS7Rewrite declaring a property creates an instance variable automatically now per the desire to cut down on pointless repetition. @JonReid instance variables couldn't be added to classes at runtime under the PowerPC and 32-bit Intel runtimes, preventing automatic creation being available as per Apple's tools. If you were targeting only 64-bit Intel you were fine. Also obviously you only get the underscore if you omit the `@synthesize` but, yeah, I'd count that as a flip flop. – Tommy Dec 07 '12 at 07:17