Supposing we have:
id value = [self valueForKey:@"frame"];
BOOL valueIsCGRect = ???;
How can I decide? Should I cast id to something?
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With