开发者

Sort Array using Desciptors throws valueForUndefinedKey

开发者 https://www.devze.com 2023-04-11 00:11 出处:网络
I\'m trying to sort an NSMutableArray of YOEvento objects. YOEvento.h @interface YOEvento : NSObject { NSString *nombre; // stores the <name> tag

I'm trying to sort an NSMutableArray of YOEvento objects.

YOEvento.h

@interface YOEvento : NSObject
{
    NSString *nombre; // stores the <name> tag
    NSDate *diaDeInicio; // stores the tag <dia-de-inicio>
    NSDate *diaDeFin; // stores the tag <dia-de-fin>
    NSString *entradilla; // stores the tag <entradilla>
}
@property (nonatomic, retain) NSString *nombre;
@property (nonatomic, retain) NSDate *diaDeInicio;
@property (nonatomic, retain) NSDate *diaDeFin;
@property (nonatomic, retain) NSString *entradilla;
@end

YOEvento.m

#import "YOEvento.h"
@implementation YOEvento
@synthesize nombre, diaDeInicio, diaDeFin, entradilla;
etc...

The array is declared in the app delegate as resumed here:

NSMutableArray *eventosParsed;
@property (nonatomic, retain) NSMutableArray *eventosParsed;
@synthesize eventosParsed;

After filling the array I'm trying to sort it by diaDeInicio:

NSSortDescriptor *descriptorByDate = [[NSSortDescriptor alloc] initWithKey:@"diaDeInicio" ascending:YES];
NSArray *descriptorsArray = [NSArray arrayWithObject:descriptorByDate];
[self.eventosParsed sortedArrayUsingDescriptors:descriptorsArray];
[descriptorByDate  release];

But after trying to run the instruction [self.eventosParsed sortedArrayUsingDescriptors:descriptorsArray]; I'm getting a SIGABRT signal and the following message in console:

'NSUnknownKeyException', reason: '[<NSCFString 0x4b564b0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key diaDeInicio.'

I have checked the contents of the array before the instruction and it seems to be filled with well formed YOEvento objects, this is a copy and paste from the debugger info on the prop开发者_开发百科erty eventosParsed first element. The values are not displayed but each instance variable in the YOEvento object has a correct value.

eventosParsed   __NSArrayM *    0x4e900e0
0   YOEvento *  0x4ea3660
    NSObject    NSObject    {...}
    nombre          NSCFString *    0x4ea37e0
    diaDeInicio __NSDate *  0x4ea7370
    diaDeFin    __NSDate *  0x4ea6630
    entradilla  NSCFString *    0x4ea3c50


You're trying to sort your array by the key @"diaDeInicio", but, as the message tells you:

-[<NSCFString 0x4b564b0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key diaDeInicio.

Strings don't have that key. You have an array of strings, so you can't sort by that key.

Fill the array with YOEvento objects instead of strings, and then you will be able to sort the array by that key.

0

精彩评论

暂无评论...
验证码 换一张
取 消