Ios 检测objective-c中名为的属性的类
我在运行时objective-c中有一个NSObject,我想知道该对象中某个属性的类,我将该属性的名称命名为NSString,我如何才能做到这一点 编辑: 自省能力课程:Ios 检测objective-c中名为的属性的类,ios,objective-c,introspection,Ios,Objective C,Introspection,我在运行时objective-c中有一个NSObject,我想知道该对象中某个属性的类,我将该属性的名称命名为NSString,我如何才能做到这一点 编辑: 自省能力课程: @implementation IntrospectionUtility // this function returns an array of names of properties + (NSMutableArray*) getProperties:(Class)class {
@implementation IntrospectionUtility
// this function returns an array of names of properties
+ (NSMutableArray*) getProperties:(Class)class
{
NSMutableArray *properties = [[NSMutableArray alloc] init];
unsigned int outCount, i;
objc_property_t *objc_properties = class_copyPropertyList(class, &outCount);
for(i = 0; i < outCount; i++) {
objc_property_t property = objc_properties[i];
const char *propName = property_getName(property);
if(propName) {
NSString *propertyName = [NSString stringWithCString:propName encoding:[NSString defaultCStringEncoding]];
[properties addObject:propertyName];
}
}
free(objc_properties);
return properties;
}
@end
我将此属性的名称命名为NSString
您可以使用函数
class\u getProperty(class cls,const char*name)
查找给定类的属性。然后使用property\u getAttributes(objc\u property\u t property)
获取属性的属性,包括编码类型字符串。阅读Objective-C Runtime Programming Guide(Objective-C运行时编程指南)的一节了解更多信息。您的意思是不调用和检查结果?为什么需要它?或者需要它的原因是什么?另外,您的字符串是NSString上某个属性的名称吗?它们没有任何属性,您需要直接检查该方法。是,字符串是属性的名称,可以是nsstring或其他类型(nsmutableArray、nsarray…)
@interface JustAnExample : NSObject
@property (nonatomic, strong) NSString *a;
@property (nonatomic, strong) NSString *b;
@property (nonatomic, strong) NSString *c;
@property (nonatomic, strong) NSString *d;
@end
@implementation JustAnExample
- (void) justAnExampleTest
{
NSMutableArray *attributes = [IntrospectionUtility getProperties:self.class];
for (NSString *attribute in attributes) {
//i want to know the type of each attributte
}
}
@end