0

I am developing a static library written in a mix of Objective C and C++. I know that in Objective C you can check if a class is available at run time by using either

NSClassFromString(@"someClass")

or

[someClass class]

I want to be able to do the same thing in a .cpp source file (not .mm because I have some core logic written in .cpp source files)

Thank you very much.

zfgo
  • 195
  • 11

2 Answers2

0

object_getClassName(id _Nullable obj) (defined in objc.h) determines if obj is an objC-Class. So the answer actually depends if your C++ library will access objC objects or not. Usually its the other way around, often you will access c++ objects in objC and pass them around or change them with the help of your c++ library. When you need to detect if c++class exists you can go this way (see link) nicely explained.

https://devblogs.microsoft.com/oldnewthing/20190710-00/?p=102678

or stick with some help of stackoverflow users here. Detect if a type exists in C++

or even better go this way How to detect existence of a class using SFINAE?

or you wanna go this way.

#include <iostream>
#include <type_traits>

class YourLovelyClass {
    //constructor..
    //destructor..
    // ... etc-pp ...
};

std::is_class<YourLovelyClass>::value
// should return true

but you see with this you can not compile if the class does not exist in your namespace or library which is why the other solutions prefer the Template feature of cpp.

Ol Sen
  • 3,163
  • 2
  • 21
  • 30
0

You can use the low-level Objective-C runtime directly:

#include <objc/runtime.h>

objc_getClass("someClass")
newacct
  • 119,665
  • 29
  • 163
  • 224