Вопрос

I have an array filled with a Cored Data object called Line. Each line has a one to one relationship with a VerticePoint entity. The VerticePoint entity contains an x and y co-ordinate.

I am trying to sort this array by the x andy co-ordinates.

If I sort by just the x vertices, like so, it works :

- (NSArray *)sortVerticesBottomLeftOrigin : (NSArray *)verticesPassed {

    NSArray *sortDescriptorsX = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"origin.x" ascending:YES]];

    NSArray *returnedVertices = [verticesPassed sortedArrayUsingDescriptors:sortDescriptorsX];

    return returnedVertices;

}

However if I add in the second sort descriptor to sort by y, the sort fails with the log error : -[__NSArrayI ascending]: unrecognized selector sent to instance 0xca7e0e0

Code :

- (NSArray *)sortVerticesBottomLeftOrigin : (NSArray *)verticesPassed {

    NSArray *sortDescriptorsX = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"origin.x" ascending:YES]];
    NSArray *sortDescriptorsY = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"origin.y" ascending:YES]];

    NSArray * finishedSort = [NSArray arrayWithObjects:sortDescriptorsX, sortDescriptorsY, nil];

    NSArray *returnedVertices = [verticesPassed sortedArrayUsingDescriptors:finishedSort];

    return returnedVertices;

}

Any ideas why ?

Это было полезно?

Решение

Try that:

- (NSArray *)sortVerticesBottomLeftOrigin : (NSArray *)verticesPassed {
    NSSortDescriptor *sortDescriptorX = [NSSortDescriptor sortDescriptorWithKey:@"origin.x" ascending:YES];
    NSSortDescriptor *sortDescriptorY = [NSSortDescriptor sortDescriptorWithKey:@"origin.y" ascending:YES];

    NSArray * finishedSort = [NSArray arrayWithObjects:sortDescriptorX, sortDescriptorY, nil];

    NSArray *returnedVertices = [verticesPassed sortedArrayUsingDescriptors:finishedSort];

    return returnedVertices;

}

The sortedArrayUsingDescriptors: method expects an array of sort descriptors. You can pass an array with multiple descriptors there. You don't have to add an array with one sort descriptor for each sort descriptor.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top