Error with NSComparator: initialization of incompatible block types

Running this NSComparator:

NSComparator comparatore = ^NSComparisonResult(NSMutableDictionary *aDictionary, NSMutableDictionary *anotherDictionary) { return [[aDictionary objectForKey:@"Item"] localizedCaseInsensitiveCompare:[anotherDictionary objectForKey:@"Item"]]; }; lista = [listaNonOrdinata sortedArrayUsingComparator:comparatore]; 

I get this error: incompatible block pointer types initialization 'int (^) (struct NSMutableDictionary *, struct NSMutableDictionary *)', expected by 'NSComparator'

I read about this error on this site and in the official guide, but I did not find a solution.

I tried everything, maybe someone here can help me, or maybe someone knows how to do the same thing differently. Thanks!

+4
source share
1 answer

NSComparator is a block waiting for two id s. You need to put id as the type of the argument and, if necessary, cast in your block (in this case, this is optional):

 NSComparator comparatore = ^NSComparisonResult(id aDictionary, id anotherDictionary) { return [[aDictionary objectForKey:@"Item"] localizedCaseInsensitiveCompare:[anotherDictionary objectForKey:@"Item"]]; }; lista = [listaNonOrdinata sortedArrayUsingComparator:comparatore]; 
+4
source

Source: https://habr.com/ru/post/1494925/


All Articles