Creating Protected and Private Variables in Objective-C Class
2014-08-22 11:48
579 查看
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
a variable or function.
Easy, each is clearly defined as such. My experience withObjective-C in iOS has not offered that simplicity.
There is an easy way to create protected and private variables fora class. This method does not utilizethe
part of Xcode. So memorymanagement is completely up to you. I findthis method works very well with variables that are not descendantsof
is no memory management needed.
The interface file (
a
that follows the line will be protected or privaterespectively. The key is not to addthe
northe
Subclasses of this sample
they will not be able to seethe
because there is no
something like
No need to go over memorymanagement in any further depth, suffice it tosay, the
example above has been
class will run into memory issues and not get released.
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
the file, every time a
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
Not complicated, however it could cause a few problems if that wasnot expected behavior... and to me, was not.
to the subclasses. The syntax is to simply adda
protectedora
privatestatementbefore
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 @synthesizedirectivethat is
part of Xcode. So memorymanagement is completely up to you. I findthis method works very well with variables that are not descendantsof
NSObjectbecausethere
is no memory management needed.
The interface file (
class.h)and inside the
@interfacedeclaration,add
a
@protectedor
@private.Anything
that follows the line will be protected or privaterespectively. The key is not to addthe
@propertylinewith the description for the variable,
northe
@synthesizeinthe 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
PhotoViewerclasswill be able to see the
indexvariable,however
they will not be able to seethe
imageViewnorthe
customView.Additionally,
because there is no
@synthesizeatplay here, retaining variables by the class will have to be donemanually. So setting the
imageViewwouldlook
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
imageViewinthe
example above has been
allocdso it must be released in the
deallocotherwisethe
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, sharedwithevery instance of the class. So using the example class above,if we created
indexinthe
@implementationpartof
the file, every time a
PhotoViewerwascreated and set the
index= 0;, every instance would havean
indexequalto
0.
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.
相关文章推荐
- public、protected、private in c++ and java
- Only Link: Inheritance — private and protected inheritance in c++
- OOP in JS, Part 1 : Public/Private Variables and Methods
- Difference between @Class and #import in Objective C
- OOP in JS Public/Private Variables and Methods
- How to Test Private and Protected methods in .NET
- Creating and Using Your Own Delegates in Objective-C
- Class and Instance Variables In Ruby
- Partial mock local private method or public method in the class and suppress static initial block
- Objective-C Primer(2)Private Methods and Class Properties
- Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
- More on Variables and Data Types(Chapter 10 of Programming in Objective-C 2.0)
- Creating custom headers and footers in Application level events using global.asax
- Creating and displaying a DataSet in Dreamweaver M
- Dynamically Creating Bound and Template Columns in GridView
- error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'
- error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'
- The longest class name in Java and .NET Framework
- Itext in Action: Creating and Manipulating PDF
- Styling Alert controls in Flex using the StyleManager class and setStyle() methods