Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:00:51+00:00 2026-05-27T02:00:51+00:00

I often find myself asserting that an object isKindOfClass of some class in Objective-C.

  • 0

I often find myself asserting that an object “isKindOfClass” of some class in Objective-C. I do it like this:

NSAssert([obj isKindOfClass:[AClass class]], @"%@ should be kind of class %@", obj, [[AClass class] description]);

I’m wondering about the best way to make a short-cut for it. I’m thinking about defining a macro, something like:

#define NSClassAssert(obj, class)  NSAssert([obj isKindOfClass:class], @"%@ should be of class %@", obj, [class description])

I’m worried that this might cause some nasty intractable compile errors or run-time problems, is there anything fundamentally wrong with doing it this way, is there a plain better way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:00:51+00:00Added an answer on May 27, 2026 at 2:00 am

    One has to ask exactly why you want to do this in the first place. You said:

    Given how tolerant and dynamic objective c is, I like to make code “abuse proof”. Instead of having an unrecognized selector message, I have a message telling me which class the object was expected to be.

    If you pass an object to a method that does not match the type declared in the signature, i.e., you do something like this:

    - (void)doSomething:(NSArray *)array { /* Do something * }
    
    - (void)doSomethingElse
    {
        NSString *string = @"my string";
        [self doSomething:string];
    }
    

    you’ll get a warning, so you should know at compile time that the object types don’t match.

    Of course, this doesn’t protect you when using a more generic type, such as id, but if you’re worried about unrecognized selectors, you really should check to see if a object responds to that selector, instead of using an assert:

    - (id)doSomething:(id)obj
    {
        if ([obj respondsToSelector:@selector(calculateSomething)]) {
            return [obj calculateSomething];
        } else {
            NSLog(@"%@ does not respond to calculateSomething", obj);
        }
    }
    

    Or use protocols to check for the availability of methods.


    However, that said, yes, your macro will work for your intended purpose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find myself writing class constructors like this: class foo: def __init__(self, arg1,
I often find myself writing a property that is evaluated lazily. Something like: if
I often find myself doing quick checks like this: if (!eregi('.php', $fileName)) { $filename
I often find myself needing reference to an object that is several objects away,
I often find myself remembering the name of a class that I want to
I often find myself into this: .class { border-top:1px dashed #0000000; border-bottom:1px dashed #0000000;
I often find myself implementing a class maintaining some kind of own status property
I often find myself wanting to debug CSS layout issues that involve DOM changes
I often find myself with a list of disconnected Linq2Sql objects or keys that
I often find myself with fairly complex data that represents something that my objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.