天天看點

Creating Protected and Private Variables in Objective-C Class

Java and other similar languages, including ActionScript, offer avery simple syntax for created protected and private variables.Protected meaning that any subclass can see and manipulate thevariable or function. Private methods and variables will not beaccessible to the subclasses. The syntax is to simply adda 

protected

ora 

private

 statementbefore a variable or function.

protected function someMethod():void { ... } private var somePrivateVar:int;  

Easy, each is clearly defined as such. My experience withObjective-C in iOS has not offered that simplicity.

Protected and Private without the 

@synthesize

There is an easy way to create protected and private variables fora class. This method does not utilizethe 

@synthesize

 directivethat is part of Xcode. So memorymanagement is completely up to you. I findthis method works very well with variables that are not descendantsof 

NSObject

 becausethere is no memory management needed.

The interface file (

class.h

)and inside the 

@interface

 declaration,add a 

@protected

or 

@private

.Anything that follows the line will be protected or privaterespectively. The key is not to addthe 

@property

 linewith the description for the variable, northe 

@synthesize

 inthe implementation file (

class.m

).

@interface PhotoViewer : UIView {   NSArray *images; // protected by default      @protected   int index; // protected in class      @private   UIImageView *imageView; // will be private in class   UIView *customView; // will be private in class } @end  

Subclasses of this sample 

PhotoViewer

 classwill be able to see the 

index

 variable,however they will not be able to seethe 

imageView

 northe 

customView

.Additionally, because there is no 

@synthesize

 atplay here, retaining variables by the class will have to be donemanually. So setting the 

imageView

 wouldlook something like

@implementation - (id)initWithFrame:(CGRect)frame {   self = [super initWithFrame:frame];   if (self) {    imageView = [[UIImageView alloc] init]; // retain count of1   }   return self; } - (void)dealloc {   [imageView release];   [super dealloc]; } @end  

No need to go over memorymanagement in any further depth, suffice it tosay, the

imageView

 inthe example above has been 

alloc

dso it must be released in the

dealloc

 otherwisethe class will run into memory issues and not get released.

Everyone Needs to Know

Static variables. Thinking you can shorthand this and createvariables directly in the implementation file would be correct. Itworks. However, creating variables in the implementation block willcreate a static variable. Static variables, are of course, sharedwith every instance of the class. So using the example class above,if we created 

index

 inthe 

@implementation

 partof the file, every time a 

PhotoViewer

 wascreated and set the 

index= 0;

, every instance would havean 

index

 equalto 

.

This can be really useful for many things, especially games wherean enemy ship may need to know how many other ships there are. Soif we used the class above and created a static variable, it wouldlook like

@implementation int numberOfPhotoViewers = 0; // static variable forPhotoViewer count - (id)initWithFrame:(CGRect)frame {   self = [super initWithFrame:frame];   if (self) {    imageView = [[UIImageView alloc] init];    numberOfPhotoViewers++;   }   return self; } - (void)dealloc {   [imageView release];   [super dealloc]; } @end  

Not complicated, however it could cause a few problems if that wasnot expected behavior... and to me, was not.