How to dynamically determine the type of an Objective-C property?

I am trying to dynamically determine the type of a property in Objective-C. Based on what I read on this site and elsewhere, I believe that I am doing the right thing. However, my code does not work.

Below is a snippet of code below. Attempting to obtain property information for "backgroundColor" and "frame", both of which are valid UIView properties, fails (class_getProperty () returns NULL):

id type = [UIView class]; objc_property_t backgroundColorProperty = class_getProperty(type, "backgroundColor"); fprintf(stdout, "backgroundColorProperty = %d\n", (int)backgroundColorProperty); // prints 0 objc_property_t frameProperty = class_getProperty(type, "frame"); fprintf(stdout, "frameProperty = %d\n", (int)frameProperty); // prints 0 

Enumerating the properties described here also does not produce the expected results. The following code:

 NSLog(@"Properties for %@", type); unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList(type, &outCount); for (i = 0; i < outCount; i++) { objc_property_t property = properties[i]; fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property)); } 

generates this output:

 2012-03-09 13:18:39.108 IOSTest[2921:f803] Properties for UIView caretRect T{CGRect={CGPoint=ff}{CGSize=ff}},R,N,G_caretRect gesturesEnabled Tc,N deliversTouchesForGesturesToSuperview Tc,N skipsSubviewEnumeration Tc,N viewTraversalMark Tc,N viewDelegate T@ "UIViewController",N,G_viewDelegate,S_setViewDelegate: inAnimatedVCTransition Tc,N,GisInAnimatedVCTransition monitorsSubtree Tc,N,G_monitorsSubtree,S_setMonitorsSubtree: backgroundColorSystemColorName T@ "NSString",&,N,G_backgroundColorSystemColorName,S_setBackgroundColorSystemColorName: userInteractionEnabled Tc,N,GisUserInteractionEnabled tag Ti,N,V_tag layer T@ "CALayer",R,N,V_layer 

Documented properties such as "backgroundColor", "frame" and others are missing, while undocumented properties such as "caretRect" and "gesturesEnabled" are included.

Any help would be greatly appreciated. In case it matters, I see this behavior on the iOS simulator. I donโ€™t know if the same thing will happen on the device itself.

Thanks Greg

+4
source share
3 answers

You get the UIView properties, the problem is that backgroundColor is not a UIView property, it is a category property. Check out UIView.h. I think you cannot get objc_category, but look at the dump class.

+6
source

Avoiding the problem a bit, the following works:

 NSMethodSignature *signature = [[UIView class] instanceMethodSignatureForSelector:@selector(backgroundColor)]; NSLog(@"%s", [signature methodReturnType]); 

Thus, the runtime might somehow lose the fact that backgroundColor is a property, but you seem to start with this information in the first code snippet so that it just checks the recipient type returned.

0
source

Category properties can be found as methods.

 @import ObjectiveC; static void test(Class class, NSString* methodName) { Method method = class_getInstanceMethod(class, NSSelectorFromString(methodName)); const char* type = method_copyReturnType(method); printf("%s : %s\n", methodName.UTF8String, type); free((void*)type); } 

Then you check some ...

 test([UILabel class], @"alpha"); test([UILabel class], @"textColor"); test([UILabel class], @"isHidden"); test([UILabel class], @"minimumScaleFactor"); 

Look at these definitions in runtime.h

 #define _C_ID '@' #define _C_CLASS '#' #define _C_SEL ':' #define _C_CHR 'c' #define _C_UCHR 'C' #define _C_SHT 's' #define _C_USHT 'S' #define _C_INT 'i' #define _C_UINT 'I' ... 

Remember to refer to the getters / seters notation for BOOL properties, instead look for "isHidden".

0
source

Source: https://habr.com/ru/post/916265/


All Articles