Objective-C 2.0:class_copyPropertyList(),如何从类别中列出属性 [英] Objective-C 2.0: class_copyPropertyList(), how to list properties from categories

查看:197
本文介绍了Objective-C 2.0:class_copyPropertyList(),如何从类别中列出属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图列出一个Objective-C类的所有属性,如 Objective-C 2.0运行时编程指南

I tried to list all properties of an Objective-C class like described in the Objective-C 2.0 Runtime Programming Guide:

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

但这只列出三个属性:

userInteractionEnabled Tc,GisUserInteractionEnabled
layer T@"CALayer",R,&,V_layer
tag Ti,V_tag



查看UIView.h的头文件这些是直接在类中声明的三个属性。其他UIView属性是通过类别添加的。

Looking at the header file for UIView.h those are the three properties directly declared in the class. The other UIView properties are added through categories.

如何获取类的所有属性,包括来自类别的属性?

How do I get all properties of a class, including those coming from categories?

我试过这个与iPhone模拟器(iPhone SDK 2.2.1),btw。 (如果这很重要)。

I tried this with the iPhone simulator (iPhone SDK 2.2.1), btw. (in case this is important).

推荐答案

根据我的测试,类别中的属性会在使用 class_copyPropertyList 。它看起来好像你在UIView上看到的属性只是描述作为属性在公共头,实际上没有声明为这样,当构建UIKit本身。

Based on my tests here, properties from categories will show up when using class_copyPropertyList. It looks as though the properties you're seeing on UIView are only described as properties in the public headers, not actually declared as such when building the UIKit itself. Probably they adopted the property syntax to make the creation of the public headers a little quicker.

为了参考,这里是我的测试项目:

For reference, here's my test project:

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface TestClass : NSObject
{
    NSString * str1;
    NSString * str2;
}
@property (nonatomic, copy) NSString * str1;
@end

@interface TestClass (TestCategory)
@property (nonatomic, copy) NSString * str2;
@end

@implementation TestClass
@synthesize str1;
@end

@implementation TestClass (TestCategory)

// have to actually *implement* these functions, can't use @synthesize for category-based properties
- (NSString *) str2
{
    return ( str2 );
}

- (void) setStr2: (NSString *) newStr
{
    [str2 release];
    str2 = [newStr copy];
}

@end

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

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

    [pool drain];
    return 0;
}

这里是输出:

str2 T@"NSString",C
str1 T@"NSString",C,Vstr1

这篇关于Objective-C 2.0:class_copyPropertyList(),如何从类别中列出属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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