Search code examples
iosstructcore-graphicscgrectkvc

How to check if an id points to a CGRect?


Supposing we have:

id value = [self valueForKey:@"frame"];
BOOL valueIsCGRect = ???;

How can I decide? Should I cast id to something?


Solution

  • The returned value will be of type NSValue for scalar types, which provides the method objCType, which returns the encoded type of the wrapped scalar type. You can use @encode() to get the encoding for an arbitrary type, and then compare the objCType.

    if(strcmp([value objCType], @encode(CGRect)) == 0)
    {
       // It's a CGRect
    }