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 8452193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:26:19+00:00 2026-06-10T11:26:19+00:00

Is there an Objective-C equivalent of C++’s dynamic_cast? It can be faked using this:

  • 0

Is there an Objective-C equivalent of C++’s dynamic_cast?

It can be faked using this:

MyClass *safeObject = [object isKindOfClass: [MyClass class]]
                      ? (MyClass *)originalObject : nil;

But this is a lot of code to type, even if I don’t need to type it often.

I am a bit rusty so this might not be quite right, but I believe the equivalent in C++ would be:

MyClass safeObject = dynamic_cast<MyClass>(orginalObject);

The context here is a block where the parameter is defined as a type of a more generic class, but in this block I “know” it’s a specific subclass. Nevertheless, I don’t want to just cast it blindly with (MyClass *)originalObject and ignore a theoretically possible error.

To be clear, while I’d love a dynamic_cast, I’d be happy with an alternate approach to safely handle this case as well.

  • 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-06-10T11:26:20+00:00Added an answer on June 10, 2026 at 11:26 am

    If you’re willing to use Objective-C++, you can write this pretty easily:

    template<typename T> inline T* objc_cast(id from) {
        if ([from isKindOfClass:[T class]]) {
            return static_cast<T*>(from);
        }
        return nil;
    }
    

    This should behave exactly as dynamic_cast<> except for obj-c objects.


    If you want to stick with vanilla Obj-C you can get similar behavior with a class method on NSObject:

    @interface NSObject (Cast)
    + (instancetype)cast:(id)from;
    @end
    
    @implementation NSObject (Cast)
    + (instancetype)cast:(id)from {
        if ([from isKindOfClass:self]) {
            return from;
        }
        return nil;
    }
    @end
    

    This version just isn’t as nice to use since you have to say something like

    UIButton *button = [UIButton cast:someView];
    

    In both versions the resulting value is nil if the cast fails.

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

Sidebar

Related Questions

Is there an equivalent of the .NET ManualResetEvent class available for use in Objective-C
Is there a way in objective-c/Cocoa to alloc an object when the class name
I'm wondering if there is an Objective C equivalent to .Net's BitConverter.GetBytes() method. For
I'm making an API in Objective-C. Is there an equivalent in Objective-C to Android's
Is there an equivalent (or vaguely similar) to PHP's strtotime in Objective C/iPhone? strtotime
Is there is an equivalent in C or Objective-C to do the following (taken
I'm aware that in Objective C I can encrypt using AES128 through their native
Is there an equivalent to a class method in C++? I know that in
NSLog puts something into stderr, is there a Objective-c method that can put NSString
In other languages such as objective c there are certain files that you can't

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.