代码之家  ›  专栏  ›  技术社区  ›  JJD

如何内联写Objective-C块?

  •  7
  • JJD  · 技术社区  · 15 年前

    indexOfObject:inSortedRange:options:usingComparator: . 这是一个例子。

    // 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块来实现上述功能。这里有两个比较函数。

    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 .

    enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
    typedef NSInteger NSComparisonResult;
    typedef NSComparisonResult (^NSComparator)(id obj1, id obj2);
    
    2 回复  |  直到 15 年前
        1
  •  21
  •   ughoavgfhw    14 年前

    可以将块定义为全局变量,以获得类似于函数的效果。

    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;
    };
    

    然后,在进行比较的方法中:

    int index1 = [array indexOfObject:number 
                        inSortedRange:NSMakeRange(0, [array count]) 
                              options:NSBinarySearchingFirstEqual 
                      usingComparator:globalBlock]; 
    

    NSComparisonResult (^globalBlock)(id,id);
    
        2
  •  2
  •   Adam    15 年前

    我知道这是旧的,但我只是跑过它,我一直试图在我的街区foo,所以这里去。。。

    我创建了一个将NSComparator作为块返回的方法。看起来是这样的:

    -(NSComparisonResult (^) (id lhs, id rhs))compareNSNumber{
    
    return [[^(id lhs, id rhs)
             {
                 return [lhs intValue] < [rhs intValue] ? (NSComparisonResult)NSOrderedAscending : [lhs intValue] > [rhs intValue] ? (NSComparisonResult)NSOrderedDescending : (NSComparisonResult)NSOrderedSame;
    
             } copy ] autorelease];
    }
    

    // Run binary search.
    int index1 = [array indexOfObject:number 
                        inSortedRange:NSMakeRange(0, [array count]) 
                              options:NSBinarySearchingFirstEqual 
                      usingComparator:[self compareNSNumber]];
    NSTimeInterval stop1 = [NSDate timeIntervalSinceReferenceDate]; 
    NSLog(@"Binary: Found index position: %d in %f seconds.", index1, stop1 - start);
    

    我得到了与原始实现非常相似的输出,在方法调用中有块定义。

    推荐文章