有没有办法获得某种类的属性数组?例如,如果我有这样的界面
@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]???)
我需要确定一个对象的属性(通过名称传递)类型,以便从XML执行反序列化.我有一些通用的伪代码(但是我不确定如何在Objective-C中执行这些比较):
id object = [[[Record alloc] init] autorelease];
NSString *element = @"date";
NSString *data = @"2010-10-16";
objc_property_t property = class_getProperty([object class], [element UTF8String]);
const char *attributes = property_getAttributes(property);
char buffer[strlen(attributes) + 1];
strcpy(buffer, attributes);
char *attribute = strtok(buffer, ",");
if (*attribute == 'T') attribute++; else attribute = NULL;
if (attribute == NULL);
else if (strcmp(attribute, "@\"NSDate\"") == 0) [object setValue:[NSDate convertToDate:self.value] forKey:element];
else if (strcmp(attribute, "@\"NSString\"") == 0) [object setValue:[NSString convertToString:self.value] forKey:element];
else if (strcmp(attribute, "@\"NSNumber\"") == 0) [object setValue:[NSNumber …Run Code Online (Sandbox Code Playgroud)