Strict Type Checking in Objective-C via Macros

前端 未结 3 1919
我在风中等你
我在风中等你 2021-01-15 16:08

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

PageTopBottom *newPage = [notification object];
assert([new         


        
3条回答
  •  迷失自我
    2021-01-15 16:36

    Hmm, with Objective-C++ there are two options:

    1. Write a template function

      template void assertType(T* obj) { ... }

    2. For a pointer X* x, use NSClassFromString([NSString stringWithUTF8String:typeid(*x).name()]).

    Without using C++, you might be able to use GCC extension typeof, but I'm not sure if [typeof(*x) class] is a legit operation...

提交回复
热议问题