简体   繁体   中英

Incompatible pointer types sending 'NSSortDescriptor *__strong' to parameter of type 'NSArray *

I am sorting my NSSet using :

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[[testSet.questions allObjects] sortedArrayUsingDescriptors:descriptor]; // warning

But it causes warning :

Incompatible pointer types sending 'NSSortDescriptor *__strong' to parameter of type 'NSArray *

您正在传递期望数组的单个描述符,请尝试以下操作:

[testSet.questions allObjects] sortedArrayUsingDescriptors:@[descriptor]];

You should see this method. It requires you to pass an Array object to it, not a NSSortDescriptor object.

  [sortDescriptions sortedArrayUsingDescriptors:<#(NSArray *)#>]

so you have to create an array and put your NSSortDescriptor object in it. Try this

  NSArray *_sortArray = [[NSArray alloc] initWithObjects:descriptor, nil];
  [testSet.questions allObjects] sortedArrayUsingDescriptors:_sortArray];

  or

  [testSet.questions allObjects] sortedArrayUsingDescriptors:@[descriptor]];

see if it could help:)

NSSortDescriptor should be passed in an array. So you have to create an array then add the descriptor in the array. Now you should pass the array instead of NSSortDescriptor :

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *descriptorArray = [NSArray arrayWithObject:descriptor]
[[testSet.questions allObjects] sortedArrayUsingDescriptors:descriptorArray];

Try this :

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[[testSet.questions allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM