Ios 如何获取作为UIView子类的ViewController的所有属性
我想使用反射来获取Ios 如何获取作为UIView子类的ViewController的所有属性,ios,objective-c,reflection,uiview,Ios,Objective C,Reflection,Uiview,我想使用反射来获取ViewController的所有属性,它们是UIView的子类 我使用此方法获取所有属性: unsigned count; objc_property_t *properties = class_copyPropertyList([self class], &count); NSMutableArray *rv = [NSMutableArray array]; unsigned i; for (i = 0; i < count; i++) { ob
ViewController
的所有属性,它们是UIView
的子类
我使用此方法获取所有属性:
unsigned count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableArray *rv = [NSMutableArray array];
unsigned i;
for (i = 0; i < count; i++)
{
objc_property_t property = properties[i];
NSString *name = [NSString stringWithUTF8String:property_getName(property)];
[rv addObject:name];
}
free(properties);
无符号计数;
objc_property_t*properties=class_copyPropertyList([self class],&count);
NSMutableArray*rv=[NSMutableArray];
未签名的i;
对于(i=0;i
但如何查找此属性的类型?您可以通过以下方式查找类型:
const char * propertyAttrs = property_getAttributes(property);
输出如下所示:
(lldb) p propertyAttrs
(const char *) $2 = 0x0065f74d "T@"UICollectionView",W,N,V_collectionView"
其中“T@“UICollectionView”
是属性类型
更新
我已经玩过了。这段代码并不理想,测试也不好,但它可以工作:
const char * property_getTypeString( objc_property_t property )
{
const char * attrs = property_getAttributes( property );
if ( attrs == NULL )
return ( NULL );
static char buffer[256];
const char * e = strchr( attrs, ',' );
if ( e == NULL )
return ( NULL );
int len = (int)(e - attrs);
memcpy( buffer, attrs, len );
buffer[len] = '\0';
return ( buffer );
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
unsigned count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableArray *rv = [NSMutableArray array];
unsigned i;
for (i = 0; i < count; i++)
{
objc_property_t property = properties[i];
NSString *name = [NSString stringWithUTF8String:property_getName(property)];
const char * typeString = property_getTypeString(property);
NSString *type = [NSString stringWithUTF8String:typeString];
NSCharacterSet *delimiters = [NSCharacterSet characterSetWithCharactersInString:@"\""];
NSArray *splitString = [type componentsSeparatedByCharactersInSet:delimiters];
Class classType = NSClassFromString(splitString[1]);
BOOL result = [classType isSubclassOfClass:UIView.class];
[rv addObject:name];
}
free(properties);
}
const char*property\u getTypeString(objc\u property\u t属性)
{
const char*attrs=property\u getAttributes(property);
if(attrs==NULL)
返回(空);
静态字符缓冲区[256];
常量char*e=strchr(attrs,,);
如果(e==NULL)
返回(空);
int len=(int)(e-attrs);
memcpy(缓冲区、属性、len);
缓冲区[len]='\0';
返回(缓冲);
}
-(无效)视图将显示:(BOOL)动画
{
[超级视图将显示:动画];
无符号计数;
objc_property_t*properties=class_copyPropertyList([self class],&count);
NSMutableArray*rv=[NSMutableArray];
未签名的i;
对于(i=0;i
函数property\u getTypeString是从这里借用的:也许您可以检查
runtime.h
属性\u getAttributes(属性\u名称)
例如:
// will return what type the property is
const char * propertyAttrs = property_getAttributes(property);
检查此项。我可以获取属性,但如何确定我正在查看的属性是UIView的子类?在您的情况下,属性结果将类似于(const char*)propertyAttrs=0x00007985“T@“UIView”,以及,N,V_thisPropertySix”,正如您所看到的,指定属性是UIView,因此我只需检查此属性是否具有字符串“UIView”在里面?如果这是UIButton或UILabel呢?UIView的哪些子类?是的,它仍然能够显示它是UIButton还是UILabel。试一试,看看这一行的意思是什么(const char*)$2=0x0065f74d“T@“UICollectionView”,W,N,V_collectionView“您缺少的是:如果(结果)然后添加到数组”在最后一行中,但确实这样做了,谢谢!如何为这些属性设置属性,以便以后可以在不进行反射的情况下使用该属性?