I've been learning Obj-C for a while now, and I have a fundamental question. There seems to be an inordinate number of 'self's everywhere in my code. I believe it is down to a basic question about the differences between two ways to declare and use variables.
Attached is some code that supports my question. In it, there are two variables declared (in different places) and used to do the same thing in a method: optionA and optionB. I am unsure which one to use. I thought it might be because using "property", as shown with optionB, would ensure that I get the accessors (setter and getter), but I see that optionA apparently has a setter.
I prefer optionA because it means I don't need "self" all over the place, but I want to understand the implications of using one over the other.
In a program I am working on, all variables are declared as properties (a la optionB below), but I have to use 'self' when accessing them. I am wondering if that is proper, and just an artifact of the right way to do things, or whether I am completely off the mark and should be using another form, like that of optionA.
When/Why would someone declare a variable between the curly braces of the interface (like optionA)?
I searched through the questions answered but don't find any that quite tell me what I am trying to learn here. But I can't quite find the info I am looking for in other questions or their answers.
I have a picture of the code here (but I don't have the prestige points to be allowed to link it directly into this message, apparently): http://i1127.photobucket.com/albums/l631/GenericImage/headerQueryOBJC.jpg
here are listings:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSTextField *optionA;
}
- (IBAction)happy:(id)sender;
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *optionB;
@end
implementation
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
-(IBAction)happy:(id)sender
{
[optionA setStringValue:@":-)"];
[[self optionB] setStringValue:@":)"];
}
@end