NSComparisonResult and NSComparator - what is it?

What are NSComparisonResult and NSComparator ?

I saw one of the type definitions, something like this:

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

Is it different from a function pointer?

Also, I can’t even guess what the ^ symbol means.

+7
ios objective-c iphone function-pointers objective-c-blocks
source share
3 answers

^ means a block type similar in concept to a function pointer.

 typedef NSComparisonResult (^NSComparator)(id obj1, id obj2); // ^ ^ ^ // return type of block type name arguments 

This means that the NSComparator type is a block that takes two objects of type id , called obj1 and obj2 , and returns NSComparisonResult .

In particular, NSComparator is defined in NSComparator Reference .

And to learn more about C blocks, check out this ADC article on topic programming blocks .

Example:

 NSComparator compareStuff = ^(id obj1, id obj2) { return NSOrderedSame; }; NSComparisonResult compResult = compareStuff(someObject, someOtherObject); 
+22
source share

Jacob's answer is good, however, to answer the question of β€œhow is this different from a function pointer?”:

1) A block is not a function pointer. Apple Blocks take care of how to create first-class citizens functions in C / C ++ / Objective-C. This is new for iOS 4.0.

2) Why introduce this strange concept? It turns out that functions of the first class are useful in quite a few scenarios, for example, managing pieces of work that can be performed in parallel, as in Grand Central Dispatch. Besides the GCD, the theory is important enough that whole software systems exist around it. Lisp was one of the first.

3) You will see this concept in many other languages, but with different names. For example, Microsoft.Net has lambdas and delegates (no communication with Objective-C delegates), while the most common names are probably anonymous functions or first class functions .

+7
source share
 NSComparisonResult comparisionresult; NSString * alphabet1; NSString * alphabet2; // Case 1 alphabet1 = @"a"; alphabet2 = @"A"; comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2]; if (comparisionresult == NSOrderedSame) NSLog(@"a and a are same. And the NSComparisionResult Value is %ld \n\n", comparisionresult); //Result: a and a are same. And the NSComparisionResult Value is 0 // Case 2 alphabet1 = @"a"; alphabet2 = @"B"; comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2]; if (comparisionresult == NSOrderedAscending) NSLog(@"a is greater than b. And the NSComparisionResult Value is %ld \n\n", comparisionresult); //Result: a is greater than b. And the NSComparisionResult Value is -1 // Case 3 alphabet1 = @"B"; alphabet2 = @"a"; comparisionresult = [alphabet1 caseInsensitiveCompare:alphabet2]; if (comparisionresult == NSOrderedDescending) NSLog(@"b is less than a. And the NSComparisionResult Value is %ld", comparisionresult); //Result: b is less than a. And the NSComparisionResult Value is 1 
-one
source share

All Articles