I think I got a bad book on Objective-C. It said to put variables I’m going to use and make available to other classes inside the interface brackets, and then declare a property of the same thing.
@interface appDelegate : NSObject <UIApplicationDelegate> {
NSNumber *myNumberOtherClassesCanUse;
}
@property (nonatomic, retain) NSNumber *myNumberOtherClassesCanUse;
However, I saw another question answered that made me think I didn’t have to put them inside the brackets, so I tried that and it still works fine.
I bought another book and it doesn’t explain it either, so can someone tell me or point me to documentation on when I have to put them inside the brackets AND make them properties?
A long and complicated answer:
The class fields, the things inside the interface brackets:
add actual bytes to your object. An objective-C class is really, underneath all the fluff, just a large C struct; and these are the fields of that struct. (You can even get at them with
->style C pointer syntax, although the compiler will yell at you if you do so without an@publicin there; the correct way to access these fields is by bare name like any other variable.)A property declaration like:
is obj-C 2.0 shorthand for a pair of method declarations like:
which adds nothing to your class; it just declares to the world that here are some operations that can be performed on this class. By convention, methods like this are supposed to get and set a field value, or at least behave like they do. Having methods like this lets you use the
obj.propandobj.prop = valdot-syntax to call them.A synthesize directive:
optionally creates these methods for you at runtime, using standard builtin behaviors (informed by that “nonatomic, assign” stuff), AND optionally creates a field of the same name to get and set. A directive like
@synthesize someNumber = someNumber_;names the created field “someNumber_” instead. If you declared such a field in the @interface block, like I did above, it uses that field; otherwise it quietly inserts an appropriate field of its own. This quiet field insertion is new behavior since the 4.x simulator; before that you had to declare all your fields in the @interface{} block, period.In summary: @interface{…} describes the physical fields of your object; @property()… describes ways your object is supposed to be used; and @synthesize is shorthand for implementing basic getter/setters and inserting extra fields if needed. I usually let @synthesize do its thing, and leave my @interface{} block empty until I have an actual need to declare a field explicitly, usually to look at it with the debugger.
[as of the latest SDKs, some of the above is dated. In particular, @synthesize is now optional, and instance vars can be declared in the
@implementationin the .m file.]