PDA

View Full Version : @protect & @private: visible question




Howiieque
Mar 27, 2009, 12:57 AM
@protected
If the object is self, a protected field is visible.
If the object is not self, access depends on its class:
If the object's class is the same as that of the receiver, the field is visible.
If the object inherits from the receiver's class, the field is not visible. (You can get around this by casting the object to the receiver's class.)
If the object has any other relation to the receiver's class, the field is not visible.
@private
If the object is self, a private field is visible.
If the object is not self:
If the object's class is the same as that of the receiver, the field is visible.
In all other cases, the field is not visible.
--from objective-c pocket reference
can give an detail expanation or code example, especially, "If the object is not self, access depends on its class".



danmanstx
Mar 27, 2009, 02:13 AM
basically private can only be accessed in the class by functions designed to modified this data, protected is very similar, but it can also be modified by classes that are derived from the original class. this is my c++ understanding

Howiieque
Mar 29, 2009, 10:30 AM
basically, i know a little of them. but just don't quite understand what the boo says.
"If the object's class is the same as that of the receiver, the field is visible."
i can see the other object's fields, if it is of the same class as the receiver?!

kainjow
Mar 29, 2009, 10:49 AM
That description is a bit confusing. Apple makes it pretty clear here (http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Articles/ocDefiningClasses.html) (go down to the The Scope of Instance Variables section). Basically, @private ivars are only accessible to that class, @protected are accessible to that class and all subclasses, and @public are accessible to all classes.

Howiieque
Mar 30, 2009, 11:03 AM
thank you.
you thought it confusing also.
i was afraid that i am not smart enough catch it.:)