Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 481
Geri Borbás Avatar asked Oct 17 '25 13:10

Geri Borbás


2 Answers

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
}
like image 68
JustSid Avatar answered Oct 19 '25 04:10

JustSid


CGRect is a struct, not an Objective-C object, so if you have an id, you don't have a CGRect.

What you probably have is an NSValue wrapping a CGRect. You can get the CGRect out using [value CGRectValue].

frame should certainly return a (wrapped) CGRect, but if you really need to check and make sure, you can use JustSid's answer.

like image 44
Kevin Avatar answered Oct 19 '25 03:10

Kevin