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

  • Home
  • SEARCH
  • 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 6714565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:30:38+00:00 2026-05-26T08:30:38+00:00

Occasionally, during development/debugging, I want to ensure that an object is of a certain

  • 0

Occasionally, during development/debugging, I want to ensure that an object is of a certain type:

PageTopBottom *newPage = [notification object];
assert([newPage isKindOfClass:[PageTopBottom class]]);

which I’ve worked into this

#define assertType(_var_, _class_) assert([_var_ isKindOfClass:[_class_ class]])

and

PageTopBottom *newPage = (id)[notification object];
assertType(newPage, PageTopBottom);

but now I’d like to, if possible, just use

assertType(newPage)

Is it possible to get information about a variable’s declared type from the variable?

I’m not positive that I’m framing the question correctly, but any answer that gets me to be able to assertType with one parameter would be great.

  • 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-26T08:30:38+00:00Added an answer on May 26, 2026 at 8:30 am

    Is it possible to get information about a variable’s declared type from the variable?

    No. By the time the program is running, that information is lost. In your case, newPage is just a 32 or 64 bit number that points to a bit of memory that holds an Objective-C object.

    I think your original unmacro’d version is the right thing to do here:

    assert([newPage isKindOfClass:[PageTopBottom class]]);
    

    That perfectly documents the assumption you are making i.e. that you assume newPage is an instance of PageTopBottom or one of its subclasses and it’s completely clear to anybody who understands Objective-C. Your macro version slightly obfuscates that, in that somebody coming across it in the code might beleive it is asserting that newPage is a PageTopBottom and not one of its subclasses (you could change the name of the macro to prevent that, I suppose, but I just wouldn’t bother).

    Edit

    What you could do is combine the declaration and assertion in one:

    #define DECLARE_AND_ASSERT_IS_KIND_OF_CLASS(T, V, I)    T* V = (T*)(I); assert([(V) isKindOfClass: [(T) class])
    

    which would work like this:

    DECLARE_AND_ASSERT_IS_KIND_OF_CLASS(PageTopBottom, newPage, [notification object]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that occasionally gets passed null for File objects. During normal
Occasionally I come accross a unit test that doesn't Assert anything. The particular example
I occasionally run into the problem that an application exception is thrown during the
Occasionally during debugging the debugger skips forward randomly. Sometimes I Step into a function
I'm writing an application that occasionally records the user's voice. During recording, I would
Occasionally, I've come across a webpage that tries to pop open a new window
Occasionally while attempting to save a Crystal Report that I'm working on in VS2008,
My site occasionally has fairly predictable bursts of traffic that increase the throughput by
When I do web development, I use a custom made logger that catches fatal
My database has name records that occasionally contain apostrophes, such as Joe's Bar and

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.