ani*_*hin 29 class object objective-c ios
有没有办法获得某种类的属性数组?例如,如果我有这样的界面
@interface MyClass : NSObject
@property (strong,nonatomic) UILabel *firstLabel;
@property (strong,nonatomic) UILabel *secondLabel;
@end
Run Code Online (Sandbox Code Playgroud)
我可以在不知道名字的情况下获得实施中的标签参考吗?
@implementation MyClass
-(NSArray*)getListOfAllLabels
{
?????
}
@end
Run Code Online (Sandbox Code Playgroud)
我知道我可以轻松地完成它[NSArray arrayWithObjects:firstLabel,secondLabel,nil]
,但我想用某种类枚举来做for (UILabel* oneLabel in ???[self objects]???)
小智 77
更确切地说,如果我正确地得到它,你需要动态的,运行时观察属性.做这样的事情(在self上实现这个方法,你要反省的类):
#import <objc/runtime.h>
- (NSArray *)allPropertyNames
{
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);
return rv;
}
- (void *)pointerOfIvarForPropertyNamed:(NSString *)name
{
objc_property_t property = class_getProperty([self class], [name UTF8String]);
const char *attr = property_getAttributes(property);
const char *ivarName = strchr(attr, 'V') + 1;
Ivar ivar = object_getInstanceVariable(self, ivarName, NULL);
return (char *)self + ivar_getOffset(ivar);
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
SomeType myProperty;
NSArray *properties = [self allPropertyNames];
NSString *firstPropertyName = [properties objectAtIndex:0];
void *propertyIvarAddress = [self getPointerOfIvarForPropertyNamed:firstPropertyName];
myProperty = *(SomeType *)propertyIvarAddress;
// Simpler alternative using KVC:
myProperty = [self valueForKey:firstPropertyName];
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
小智 12
使用NSObject的attributeKeys方法.
for (NSString *key in [self attributeKeys]) {
id attribute = [self valueForKey:key];
if([attribute isKindOfClass:[UILabel class]])
{
//put attribute to your array
}
}
Run Code Online (Sandbox Code Playgroud)
看看这个链接.它是客观C运行时的客观c包装器.
您可以使用以下代码
uint count;
objc_property_t* properties = class_copyPropertyList(self.class, &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
const char* propertyName = property_getName(properties[i]);
[propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
}
free(properties);
Run Code Online (Sandbox Code Playgroud)
您必须包含运行时标头
#import<objc/runtime.h>
uint propertiesCount;
objc_property_t *classPropertiesArray = class_copyPropertyList([self class], &propertiesCount);
free(classPropertiesArray);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26095 次 |
最近记录: |