Incompatible pointer types sending 'NSSortDescriptor *__strong' to parameter of type 'NSArray *
我正在使用
1 2 | NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [[testSet.questions allObjects] sortedArrayUsingDescriptors:descriptor]; // warning |
但这会引起警告:
1 | Incompatible pointer types sending 'NSSortDescriptor *__strong' to parameter of type 'NSArray * |
您正在传递期望数组的单个描述符,请尝试以下操作:
1 | [testSet.questions allObjects] sortedArrayUsingDescriptors:@[descriptor]]; |
您应该看到此方法。 它要求您传递一个Array对象,而不是NSSortDescriptor对象。
1 | [sortDescriptions sortedArrayUsingDescriptors:<#(NSArray *)#>] |
因此,您必须创建一个数组并将NSSortDescriptor对象放入其中。 尝试这个
1 2 3 4 5 6 | NSArray *_sortArray = [[NSArray alloc] initWithObjects:descriptor, nil]; [testSet.questions allObjects] sortedArrayUsingDescriptors:_sortArray]; or [testSet.questions allObjects] sortedArrayUsingDescriptors:@[descriptor]]; |
看看是否有帮助:)
尝试这个 :
1 2 | NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [[testSet.questions allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]]; |
希望这会有所帮助。
NSSortDescriptor应该在数组中传递。 因此,您必须创建一个数组,然后在该数组中添加描述符。 现在,您应该传递数组而不是NSSortDescriptor:
1 2 3 | NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *descriptorArray = [NSArray arrayWithObject:descriptor] [[testSet.questions allObjects] sortedArrayUsingDescriptors:descriptorArray]; |