如何动态确定Objective-C属性类型? [英] How to dynamically determine Objective-C property type?

查看:68
本文介绍了如何动态确定Objective-C属性类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态确定Objective-C中属性的类型.根据我在本网站及其他地方所读到的内容,我相信自己做的是正确的.但是,我的代码无法正常工作.

I'm trying to dynamically determine the type of a property in Objective-C. Based on what I have read on this site and elsewhere, I believe I am doing the right thing. However, my code isn't working.

下面的代码片段演示了该问题.尝试获取均为UIView的有效属性的"backgroundColor"和"frame"的属性信息失败(class_getProperty()返回NULL):

The code snippet below demonstrates the problem. Attempting to get the property information for "backgroundColor" and "frame", both of which are valid properties of UIView, 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 as described here doesn't produce the expected results, either. 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));
}

生成此输出:

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

缺少诸如"backgroundColor","frame"之类的文档属性,而包括诸如"caretRect"和"gesturesEnabled"之类的未文档属性.

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

任何帮助将不胜感激.如果相关,我会在iOS模拟器上看到此行为.我不知道在实际设备上是否会发生同样的事情.

Any help would be very much appreciated. In case it is relevant, I'm seeing this behavior on the iOS simulator. I don't know if the same thing would happen on an actual device.

谢谢,格雷格

推荐答案

您正在获取UIView属性,问题是backgroundColor不是UIView属性,而是类别属性.检查UIView.h.我认为您无法获得objc_category,但请查看类转储.

You are getting the UIView properties, the problem is backgroundColor is not a UIView property, is a category property. Check UIView.h. I think you can't get a objc_category, but have a look at class-dump.

这篇关于如何动态确定Objective-C属性类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆