Allison
Allison

Reputation: 2383

Sorting NSMutableArray ends with error

I'm sorting a NSMutableArray that has NSString that contain numbers.
This is the code I'm using:

//creating mutable array
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"4", @"2", @"7", @"8", nil];

//sorting
[myArray sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
    return [str1 compare:str2 options:(NSNumericSearch)];
}];

//logging
NSLog(@"%@", myArray);

When I build Xcode highlights this piece of code with an error: }]; The error is:
Incompatible block pointer types initializing 'int (^)(struct NSString *, struct NSString *)', expected 'NSComparator'

Upvotes: 0

Views: 89

Answers (1)

Stig Brautaset
Stig Brautaset

Reputation: 2632

I think you need to use id as the argument types to your block, rather than NSString. In Apple's Short Practical Guide to Blocks it says:

The Foundation framework declares the NSComparator type for comparing two items:

typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);

Upvotes: 0

Related Questions