BSD

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • object type testing

    7 answers - 1609 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    isKClass: I think tests to see if you are a subclass of a given
    class. Its what I have used before.
    This is even a bit restricting since it may not tell you all you need
    to know to be compatible. For instance, it may be that the object
    subclasses NS but still implements the protocol you need - so
    you can call conformsToProtocol: to see if this is true. If there is
    a formal protocol defined, this is probably the best way to check if
    the object is what you expect.
    May 7, 2006, at 8:41 AM, Ben Dougall wrote:
    hello,
    regarding finding out if an object instance is a particular type of
    class or not
    which should be used out of the following three?:
    if( [ isMClass:[AClass class]] ) {
    if( [ class] == [AClass self] ) {
    if( [[ className] isEqualToString:@"AClass"] ) {
    i suppose the last one is only necessary if you're dealing with
    class names in strings, otherwise not necessary. i'm currently
    using [ class] == [AClass self] but am wondering if it's K?
    thanks, ben
    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40mac.com
    This email sent to tblanchard (AT) mac (DOT) com
    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com
    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.1 | | 808 bytes | |

    hello,

    regarding finding out if an object instance is a particular type of
    class or not

    which should be used out of the following three?:

    if( [ isMClass:[AClass class]] ) {

    if( [ class] == [AClass self] ) {

    if( [[ className] isEqualToString:@"AClass"] ) {

    i suppose the last one is only necessary if you're dealing with class
    names in strings, otherwise not necessary. i'm currently using
    [ class] == [AClass self] but am wondering if it's K?

    thanks, ben

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.2 | | 797 bytes | |

    5/7/06, Ben Dougall <bend (AT) freenet (DOT) co.ukwrote:

    regarding finding out if an object instance is a particular type of
    class or not

    The basic answer is to use -isKClass:, like this:

    if ([ isKClass:[NSString class]]) {
    // Do something stringy with
    }

    However, if you just need to know whether you can call some method on
    the object, you can be more general and do this:

    if ([
    respondsToSelector:@selector()]) {
    // Call myMethod: on the
    }

    Larry

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.3 | | 1262 bytes | |

    5/7/06, Ben Dougall <bend (AT) freenet (DOT) co.ukwrote:
    Todd, thanks for the reply.

    Sunday, May 7, 2006, at 05:03 pm, Todd Blanchard wrote:

    isKClass: I think tests to see if you are a subclass of a given
    class. Its what I have used before.

    i'm not after subclasses though -- just is of a particular type or not.
    so [ class] == [AClass self] works fine but is there any reason
    i should use isMClass instead of that? (isMClass is the
    one that's appropriate for me out of isMClass and isKClass).
    -isKClass: actually returns YES if the object is a member of the
    given class or is a subclass of the given class. This is probably the
    behavior you want, because say you're testing an object that's passed
    into the method: If the user of the method passes an NSMutableString
    but your class only checks for isMClass:[NSString class], then
    your method won't work. So I would go with -isKClass:.

    Larry

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.4 | | 1335 bytes | |

    Am 07.05.2006 um 17:41 schrieb Ben Dougall:
    regarding finding out if an object instance is a particular type of
    class or not

    Just to play the party pooper here: Keep in mind that, *in general*
    it's a sign of a mistake in your design if you need to check for a
    particular class. Especially in Cocoa, it is perfectly fine that an
    object may not be of a particular class, but rather of a class that
    implements the same methods or a subclass of a particular class. Even
    more importantly: NSString and several other fundamental classes of
    Foundation are actually class clusters and the class itself is an
    abstract base class. So, e.g. checking whether an object is of class
    NSString will always return false, because you only get objects of
    NSMutableString, NSCFString or whatever

    So, if you can, use conformsToProtocol, respondsToSelector or
    similar methods to check for the actual feature you need, and not for
    a particular class name.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.5 | | 2418 bytes | |

    May 7, 2006, at 11:22 AM, Uli Kusterer wrote:

    Am 07.05.2006 um 17:41 schrieb Ben Dougall:
    >regarding finding out if an object instance is a particular type
    >of class or not
    >

    Just to play the party pooper here: Keep in mind that, *in
    general* it's a sign of a mistake in your design if you need to
    check for a particular class. Especially in Cocoa, it is perfectly
    fine that an object may not be of a particular class, but rather of
    a class that implements the same methods or a subclass of a
    particular class. Even more importantly: NSString and several other
    fundamental classes of Foundation are actually class clusters and
    the class itself is an abstract base class. So, e.g. checking
    whether an object is of class NSString will always return false,
    because you only get objects of NSMutableString, NSCFString or
    whatever

    So, if you can, use conformsToProtocol, respondsToSelector or
    similar methods to check for the actual feature you need, and not
    for a particular class name.

    Another gotcha to look out for in specific class checking is if the
    class ends up being used by Cocoa Bindings at all, the bindings
    observing goo will end up making a subclass of your class which is
    what all your actual objects will end up being. I.e. all your MyClass
    objects actually end up becoming KVMyClass objects instead.
    Similarly, fault objects or forwarding proxy objects may implement -
    isMClass: or -isKClass: to pass along the call to the
    'real' object involved since they are acting as if they are some
    other class.

    Comparing specific class pointers or class names will break in all of
    these sorts of situations where other code is playing tricks with the
    runtime. Since things like key-value-observing are most definitely
    not local to your own code, and you can't guarantee that Apple won't
    change implementations of Cocoa features to play more of these sorts
    of tricks in the future, be VERY careful about specific class checks.

    Hope this helps,
    - Greg

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.6 | | 977 bytes | |

    May 7, 2006, at 12:03 PM, Todd Blanchard wrote:

    This is even a bit restricting since it may not tell you all you
    need to know to be compatible. For instance, it may be that the
    object subclasses NS but still implements the protocol you
    need - so you can call conformsToProtocol: to see if this is true.
    If there is a formal protocol defined, this is probably the best
    way to check if the object is what you expect.

    If you're using a formal protocol, you can also check at compile-
    time, by declaring it as part of the type:
    -(void)foo: (id<ProtocolName>)bar;

    sherm--
    Cocoa programming in Perl:
    Hire me! My resume: http://www.dot-app.org

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.7 | | 1679 bytes | |

    May 7, 2006, at 12:56 PM, Lawrence Sanbourne wrote:
    5/7/06, Ben Dougall <bend (AT) freenet (DOT) co.ukwrote:
    >Todd, thanks for the reply.
    >>

    >Sunday, May 7, 2006, at 05:03 pm, Todd Blanchard wrote:
    >>

    >isKClass: I think tests to see if you are a subclass of a
    >given
    >class. Its what I have used before.
    >>

    >i'm not after subclasses though -- just is of a particular type or
    >not. []
    >

    -isKClass: actually returns YES if the object is a member of the
    given class or is a subclass of the given class. This is probably the
    behavior you want,

    Ben specifically said he doesn't want the -isKClass: test, and
    the code options in his original message are consistent with this.
    Since we don't know what the context is, we don't have enough
    information to second-guess.

    *Assuming* specific class membership is the desired test, offhand I
    would use -isMClass: instead of comparing class objects, just
    because I think it reads better, but I doubt it makes any practical
    difference. Conceivably for debugging it might be useful to break on
    -isMClass:, or replace it via +poseAsClass:, but if your
    situation is very simple it probably doesn't matter.

    Do not post admin requests to the list. They will be ignored.
    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    Help/Unsubscribe/Update your Subscription:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com

Re: object type testing


max 4000 letters.
Your nickname that display:
In order to stop the spam: 5 + 4 =
QUESTION ON "BSD"

EMSDN.COM