获取属性名称的属性类 [英] Get property class giving the property name

查看:144
本文介绍了获取属性名称的属性类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的



我有一堆从我的数据模型生成的继承自NSManagedObject的类。我需要一种方法来获得,对于任何一个类,一个字符串的属性名称作为键,字符串的类型作为值,为数组中传递的每个属性名称的字典。
或更好的代码:

  //我有什么 - 生成的代码
@interface ClassA: NSManagedObject

@property(nonatomic,retain)ClassX * propX;
@property(nonatomic,retain)ClassY * propY;

@end

@implementation ClassA

@dynamic propX;
@dynamic propy;

@end

//我需要的
//方法
- (NSDictionary *)dictFromPropNameToPropType:(NSArray *)props {
// dict就像@ {@propX:@ClassX,@propY:@ClassY};
return dict;
}

// call
dictFromPropNameToPropType(@ [@propX,@propy]);

字典创建的逻辑在我身上。我需要一个方法来获取属性类名称作为字符串,给它的名称。



我尝试了

  //我的ClassA的实例方法
SEL selector = NSSelectorFromString(propertyNameAsString); // like @propX
id object = [self performSelector:selector];
Class class = [object class];
NSString * className = NSStringFromClass(class);

并尝试使用 dictionaryWithValuesForKeys 它似乎使用相同的机制,并导致相同的错误。



没有成功。我得到的传递为 propertyNameAsString 的密钥的类不键值编码兼容错误,即使你有 propX 我的ClassA中的属性



我研究的内容



看过关于KVC和KVO的苹果教程,还有关于运行时的教程(了解关于@dynamic自动生成属性的内容)。



有了Martin R的回答,我有这个代码:

  NSMutableDictionary * result = [NSMutableDictionary new]; 
NSEntityDescription * selfEntity = [self entity];
NSDictionary * relationshipsByName = selfEntity.relationshipsByName;
for(NSString * relationDescription in relationshipsByName){
NSRelationshipDescription * relationshipDescription = relationshipsByName [relationDescription];
NSEntityDescription * destinationEntity = relationshipDescription.destinationEntity;
result [relationDescription] = destinationEntity.managedObjectClassName;
}


解决方案

c $ c> NSManagedObject ,所以你可以检查对象 entity ,这是
a NSEntityDescription




  • 从实体描述中获取 propertiesByName ,这是
    a字典,属性名称为键。值为 NSAttributeDescription
    NSRelationshipDescription 对象。


  • NSAttributeDescription 有一个方法 attributeValueClassName ,它是类
    ,作为字符串)。


  • NSRelationshipDescription 有一个方法 destinationEntity 其描述目标
    实体并且具有 managedObjectClassName 方法。



What I need

I have a bunch of classes generated from my data model that inherit from NSManagedObject. I need a way to get, for any of theses classes, a dictionary of a string of a property name as the key, and the string of it's type as the value, for every property name passed in an array. Or better, in code:

// what I have - generated code
@interface ClassA : NSManagedObject

@property (nonatomic, retain) ClassX *propX;
@property (nonatomic, retain) ClassY *propY;

@end

@implementation ClassA

@dynamic propX;
@dynamic propy;

@end

// what I need
// the method
-(NSDictionary*)dictFromPropNameToPropType:(NSArray*)props {
    //dict being something like @{ @"propX" : @"ClassX", @"propY" : @"ClassY" };
    return dict;
}

// call
dictFromPropNameToPropType(@[@"propX", @"propY"]);

The logic of the dictionary creation is on me. I need a way to get the property class name as a string, giving it's name.

What I've tried

// a instance method of my ClassA
SEL selector = NSSelectorFromString(propertyNameAsString); // like @"propX"
id object = [self performSelector:selector];
Class class = [object class];
NSString *className = NSStringFromClass(class);

and also tried something with dictionaryWithValuesForKeys, but it seems to use the same mechanism and result in the same kind of errors.

No succes there. I get the "class not key value coding-compliant" error for the key passed in as propertyNameAsString, even thou I have the propX property in my ClassA

What I've researched

I've looked the Apple tutorials about KVC and KVO, and also the one about the Runtime (to learn something about the @dynamic auto-generated property). But didn't figure it out on my own.

Edit

With Martin R's answer, I've got this code:

NSMutableDictionary *result = [NSMutableDictionary new];
NSEntityDescription *selfEntity = [self entity];
NSDictionary *relationshipsByName = selfEntity.relationshipsByName;
for (NSString *relationDescription in relationshipsByName) {
    NSRelationshipDescription *relationshipDescription = relationshipsByName[relationDescription];
    NSEntityDescription *destinationEntity = relationshipDescription.destinationEntity;
    result[relationDescription] = destinationEntity.managedObjectClassName;
}

解决方案

You have subclasses of NSManagedObject, so you can inspect the objects entity, which is a NSEntityDescription.

  • From the entity description, get the propertiesByName, which is a dictionary with the property names as key. The values are NSAttributeDescription or NSRelationshipDescription objects.

  • NSAttributeDescription has a method attributeValueClassName, which is the class representing the attribute (as a string).

  • NSRelationshipDescription has a method destinationEntity which describes the target entity and has a managedObjectClassName method.

这篇关于获取属性名称的属性类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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