Using the performSelector function: to access the BOOL property

I am using performSelector: which returns an id object to call several other methods. The return type of these methods can be either BOOL , int , NSDate , or any other kind of object.

How can I find out if an object is returned from performSelector: BOOL or not? I tried converting it to NSNumber , etc., but it will work if the object is not BOOL .

I have a class with attributes such as:

 @property(retain,nonatomic) NSString* A; @property(assign,nonatomic) BOOL B; @property(retain,nonatomic) NSArray* C; @property(assign,nonatomic) int64_t D; 

This class is generated by the framework, so I cannot change it. But I want to iterate over A , B , C , D to call each attribute and retrieve the data. However, as you can see, the type of return may vary, and I need to adapt to this.

I am doing something similar to:

 SEL s = NSSelectorFromString(@"A"); id obj = [object performSelector:s]; //check if obj is BOOL //do something with obj 
+4
source share
1 answer

If you just need to get the values ​​of various properties, use a key encoding that automatically wraps scalar types such as int and BOOL in NSNumber cases. So you need the following line:

 id value = [object valueForKey:@"somePropertyName"]; 

Otherwise, you can check the return type in advance by calling methodSignatureForSelector: on the target, but this seems like a bunch of unnecessary work, given the situation you described.

+9
source

All Articles