On 3/16/12 2:00 PM, Brian Lambert wrote:
> This means that my UILabel called labelMyLabel is publicly available.
> Anyone who has access to an instance of MyViewController can do anything
> they want to with my label, including replacing it.
> 
> Also, anyone who has an instance of MyViewController can call my
> buttonDoItTouchUpInside action.

In addition to David's remarks, it should also be noted that there isn't
really any concept of "private" properties or methods (in the enforced
sense) in Objective-C due to the dynamic nature of the language and
runtime.  No matter where you *declare* your properties and/or methods,
what you state above would always be possible.

(As an aside, major features in Cocoa rely on fairly crazy runtime
manipulation.  For example, key-value observing:
http://mikeash.com/pyblog/friday-qa-2009-01-23.html)

Suppose you wanted to peek under a class' hood (for curiosity's sake, of
course; private API usage is generally a bad idea and is explicitly
forbidden in the App Stores and from discussion on the official mailing
lists).  To see a class' properties (both from itself and its protocols)
you could try something along the lines of (warning: a thrown together
quick hack, probably has bugs):

    unsigned int propertyCount;
    objc_property_t *allProperties = class_copyPropertyList([MyClassName
class], &propertyCount);
    for (NSUInteger i = 0; i < propertyCount; i++) {
        const char *name = property_getName(allProperties[i]);
        NSLog(@"%s", name);
    }

    unsigned int protocolCount;
    Protocol **allProtocols = class_copyProtocolList([MyClassName
class], &protocolCount);
    for (NSUInteger i = 0; i < protocolCount; i++) {
        const char *protocol = protocol_getName(allProtocols[i]);
        NSLog(@"PROTOCOL %s", protocol);
        unsigned int protoPropertyCount;
        objc_property_t *protoProperties =
protocol_copyPropertyList(allProtocols[i], &protoPropertyCount);
        for (NSUInteger j = 0; j < protoPropertyCount; j++) {
            const char *propName = property_getName(protoProperties[j]);
            NSLog(@"\t%s", propName);
        }
    }

This and other fun hackery is documented in the Runtime Programming
Guide:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide



-- 
Conrad Shultz

Synthetiq Solutions
www.synthetiqsolutions.com
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to