如何内联编写 Objective-C 块?

How to write Objective-C Blocks inline?(如何内联编写 Objective-C 块?)
本文介绍了如何内联编写 Objective-C 块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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);   

我想知道如何将外部定义的 Objective-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;
}

这些是参考 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 块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to stop UIBarButtonItem text from truncating?(如何阻止UIBarButtonItem文本被截断?)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
NSURLSessionTaskPriority seems to be ignored?(NSURLSessionTaskPriority似乎被忽略了?)
How to make dataWithEPSInsideRect vector rather than bitmap in vector format?(如何用EPSInside Rect将dataWithEPSInside Rect变成矢量而不是位图的矢量格式?)
HTTPS request using volley(使用 volley 的 HTTPS 请求)