Objective C Class Properties trying to understand a bit more.

As I move into more direct programming with Obj-C and making my own Classes
(usually Controllers or Delegate of other Classes that I’ve found and downloaded)

I have a question about properties. As many of the Classes and examples that I find
seem to do things quite differently.

What is the difference between Classes that implement something like:



@interface Class : NSView 
{	
	IBOutlet NSColorWell				*myBackgroundWell;
	IBOutlet NSColorWell				*myLineWell;
	
	IBOutlet NSButton					*myHoldButton;
	
	NSColor								*backgroundColor;
	NSColor								*lineColor;
	
	CGPoint*							waveformPoints;
	UInt32								waveformPointsCount;
}

- (void) setBackgroundColor:(NSColor*) color;
- (void) setLineColor:(NSColor*) color;

- (IBAction) changeBackgroundColor: (id) sender;
- (IBAction) changeLineColor: (id) sender;

@end


and something like:

@interface SubClass : Class


@property (nonatomic, assign) BOOL shouldOptimizeForRealtimePlot;
@property (nonatomic, assign) BOOL shouldCenterYAxis;
@property (nonatomic, strong) EZAudioPlotWaveformLayer *waveformLayer;

// bunch of methods

@end

THIRD - which uses @synthesize in method file

@interface AppDelegate2 : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window2;
@property (assign) IBOutlet WaveFormViewOSX *wfv2;

- (IBAction)loadAudioFile:(id)sender;

@end

////////////////////// and in it’s .m file

@implementation AppDelegate2

@synthesize window2 = _window;
@synthesize wfv2 = _wfv;

// some methods
@end

FORTH: and some that have their properties both in the brackets and defined

@interface ClassFour : NSView 
{	
	IBOutlet NSColorWell				*myBackgroundWell;

	NSColor								*backgroundColor;
	NSColor								*lineColor;	
	CGPoint*							waveformPoints;
	UInt32								waveformPointsCount;
        NSString*                                                  stringOne;
         NSString*                                                  stringTwo;
}

@property (assign) IBOutlet NSColorWell * myBackgroundWell;
@property (assign) NSColor * backgroundColor;
@property (assign) NSColor * lineColor;
@property (assign) CGPoint * waveformPoints;
@property (assign) NSColor * lineColor;
@property (assign) NSString * stringOne;
@property (assign) NSString * stringTwo;

@property (assign) NSString * stringThree;
@property (assign) CGFloat songLength;

@end

I understand with @synthesize that compiler will automatically implement the
setters and getters for us.


Are the properties in the { } brackets instance variable of the Class?

And then say the properties stringThree and songLength local Class Variables
that are only available inside the class? IE I can’t ask for

ClassFourInstance.stringThree

Next about instance variables in my methods:

when I want to be referring to the Instance Variable of stringTwo in one of my methods.
Do I need to make sure that I writing it like:

NSString* newStringFour = [_stringTwo stringByAppendingString:(@" - Completed")];

and what is the difference here if I was to use “self”:

NSString* newStringFive = [self.stringTwo stringByAppendingString:(@" - Not Completed")];

and if I’m using the

@synthesize stringTwo = _stringTwo; 

or Which I understand is kinda shorthand for the above and will automatically
synthesize it to _stringTwo

@synthesize stringTwo; 

will then using stringTwo in my methods always then refer to the instances
“version” of stringTwo? am I able to just write:

NSString* newStringSix = [stringTwo stringByAppendingString:(@" - V06")];

Is the using the underscore just a way of keeping track in your own head
that your using an instance varable?

How does “self” come into play when the class is being used as an instance?

thanks for help.

Kerry

The differences you see mainly reflect when the code was written. Objective-C never used to have properties. You set up instance variables (the variables inside the {}) and wrote your own setter and getter methods. Properties removed that requirement, although “under the hood” they do the same thing.

Over time they’ve changed a bit, so now a property xyz is assumed to be backed by an ivar _xyz (this can be overridden via a @synthesize statement).

The bit in parens where it says assign or strong is to do with how the memory is to be managed, something that used to be done in the setters and getter. With ARC, C types are generally assign, while objects are either strong, copy, or weak. With manual memory management, objects were also generally assign.

Lastly, you use self to refer to the instance itself. So generally you refer to a property within its class as self.propertyName or or even [self propertyName]. Referring this way rather than via the underlying instance variable directly means memory management is handled correctly and key-value observing is effective, but it’s not always necessary.

Thanks that clears up a lot