如何编写Objective-C块内联? [英] How to write Objective-C Blocks inline?
问题描述
我想使用objective-c块实现二进制搜索。我使用函数 indexOfObject:inSortedRange:options:usingComparator:
。这是一个例子。
I am trying to implement a binary search using objective-c blocks. I am using the function indexOfObject:inSortedRange:options:usingComparator:
. Here is an example.
// A pile of data.
NSUInteger amount = 900000;
// A number to search for.
NSNumber* number = [NSNumber numberWithInt:724242];
// Create some array.
NSMutableArray* array = [NSMutableArray arrayWithCapacity:amount];
for (NSUInteger i = 0; i < amount; ++i) {;
[array addObject:[NSNumber numberWithUnsignedInteger:i]];
}
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
// Run binary search.
int index1 = [array indexOfObject:number
inSortedRange:NSMakeRange(0, [array count])
options:NSBinarySearchingFirstEqual
usingComparator:^(id lhs, id rhs) {
if ([lhs intValue] < [rhs intValue]) {
return (NSComparisonResult)NSOrderedAscending;
} else if([lhs intValue] > [rhs intValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSTimeInterval stop1 = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Binary: Found index position: %d in %f seconds.", index1, stop1 - start);
// Run normal search.
int index2 = [array indexOfObject:number];
NSTimeInterval stop2 = [NSDate timeIntervalSinceReferenceDate];
NSLog(@"Normal: Found index position: %d in %f seconds.", index2, stop2 - start);
我不知道如何使用外部定义的目标c块与上述函数。这里有两个比较函数。
I wonder how I can use an externally defined objective-c block with the aforementioned function. Here are two compare functions.
NSComparisonResult compareNSNumber(id lhs, id rhs) {
return [lhs intValue] < [rhs intValue] ? NSOrderedAscending : [lhs intValue] > [rhs intValue] ? NSOrderedDescending : NSOrderedSame;
}
NSComparisonResult compareInt(int lhs, int rhs) {
return lhs < rhs ? NSOrderedAscending : lhs > rhs ? NSOrderedDescending : NSOrderedSame;
}
这些是参考下面的声明c $ c> NSObjCRuntime.h 。
Those are written in reference to the following declarations which can be found in NSObjCRuntime.h
.
enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
typedef NSInteger NSComparisonResult;
typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
推荐答案
您可以将块定义为全局变量类似函数的效果。
You can define a block as a global variable to get an effect similar to functions.
NSComparisonResult (^globalBlock)(id,id) = ^(id lhs, id rhs) {
if([lhs intValue] < [rhs intValue]) {
return (NSComparisonResult)NSOrderedAscending;
} else if([lhs intValue] > [rhs intValue]) {
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
};
然后,在执行比较的方法中:
Then, in the method doing the comparison:
int index1 = [array indexOfObject:number
inSortedRange:NSMakeRange(0, [array count])
options:NSBinarySearchingFirstEqual
usingComparator:globalBlock];
将块放在标题中供外部使用:
To put the block in a header, for external use:
NSComparisonResult (^globalBlock)(id,id);
这篇关于如何编写Objective-C块内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!