Gre*_*own 4 properties objective-c objective-c-runtime ios
我正在尝试动态确定Objective-C中属性的类型.根据我在本网站和其他地方的内容,我相信我做的是正确的.但是,我的代码无效.
下面的代码片段演示了这个问题.尝试获取"backgroundColor"和"frame"的属性信息(两者都是UIView的有效属性)失败(class_getProperty()返回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
Run Code Online (Sandbox Code Playgroud)
描述枚举的属性在这里没有产生预期的结果,无论是.以下代码:
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));
}
Run Code Online (Sandbox Code Playgroud)
生成此输出:
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
Run Code Online (Sandbox Code Playgroud)
缺少记录的属性,例如"backgroundColor","frame"和其他属性,而包括"caretRect"和"gesturesEnabled"等未记录的属性.
任何帮助将非常感谢.如果它是相关的,我在iOS模拟器上看到这种行为.我不知道在实际设备上是否会发生同样的事情.
谢谢,格雷格
您正在获取UIView属性,问题是backgroundColor不是UIView属性,是类别属性.检查UIView.h.我想你不能得到一个objc_category,但看看class-dump.
归档时间: |
|
查看次数: |
3997 次 |
最近记录: |