Reputation: 6384
I have a viewcontroller which displays Core Data entities in a UIPicker. Select a row and that entity is passed (as a property) to another viewcontroller for displaying.
I have a method that returns all the property names of the Player entity:
- (NSArray *)allPropertyNames {
unsigned count;
objc_property_t *properties = class_copyPropertyList([Player 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;
}
and then I was trying to display the values of those properties this way:
for(NSString* strThisField in arrFields) {
UILabel* lblThisField = [[UILabel alloc] initWithFrame:CGRectMake(0.0, fieldY, vPlayerBox.frame.size.width, 30.0)];
lblThisField.text = [Player valueForKeyPath:strThisField];
lblThisField.textColor = AO_Brown;
lblThisField.font = AO_SansSerif_Font;
[vPlayerBox addSubview:lblThisField];
fieldY = fieldY + 35.0;
}
But I am crashing on the valueForKeyPath line so obviously I am doing something wrong. Is there a way to get property values this way or am I going down a dead end?
Thanks
Edit:
I had an error in my UILabel.text property - should of read lblThisField.text = [self.myPlayer getValueForKey:strThisKey]; but even changing that it would still crash. I wrote a second method to get the values and keys into a dictionary and though it works it seems like a clunky solution.
- (NSDictionary*) getKeyValuesOfEntity : (Player*) thisEntity {
NSArray* arrProperties = [self getProperties];
NSMutableDictionary* dictKeysAndValues = [[NSMutableDictionary alloc] init];
for(NSString* strThisKey in arrProperties) {
id value = [thisEntity valueForKey:strThisKey];
if ([value isKindOfClass:[NSString class]]) {
NSString* strValue = (NSString*)value;
[dictKeysAndValues setObject:strValue forKey:strThisKey];
} else if ([value isKindOfClass:[NSDate class]]) {
NSDate* dtValue = (NSDate*)value;
[dictKeysAndValues setObject:dtValue forKey:strThisKey];
} else if ([value isKindOfClass:[NSNumber class]]) {
NSNumber* numValue = (NSNumber*)value;
[dictKeysAndValues setObject:numValue forKey:strThisKey];
} else if ([value isKindOfClass:[NSSet class]]) {
NSSet* setValue = (NSSet*)value;
[dictKeysAndValues setObject:setValue forKey:strThisKey];
}
}
return dictKeysAndValues;
}
- (NSArray *)getProperties {
unsigned count;
objc_property_t *properties = class_copyPropertyList([Player 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;
}
Upvotes: 0
Views: 98
Reputation: 3885
I don't know "Player" in your for-in loop stands for a class or an instance. The method -[NSObject valueForKeypath] is an instance method. So you need to initialize an instance of class Player by doing this:
Player *player = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:appDelegate.managedObjectContext];
And everything will go cool. Hope this will help.
Upvotes: 1