Shafa95
Shafa95

Reputation: 201

Error with NSComparator: incompatible block pointer types initializing

Doing 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 initializing 'int (^)(struct NSMutableDictionary *, struct NSMutableDictionary *)', expected 'NSComparator'

I've read about this error on this site and on the official guide, but I have not found the solution.

I've tried everything, maybe someone here can help me, or maybe someone knows how to do the same sort in another way. Thanks!

Upvotes: 4

Views: 1053

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

NSComparator is a block expecting two ids. You need to put id as the argument type, and do casting inside your block if necessary (in this case, it is not necessary):

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

Upvotes: 4

Related Questions