[Objective-C] 睡眠排序法-objective C版 →→→→→进入此内容的聊天室

来自 , 2021-05-03, 写在 Objective-C, 查看 207 次.
URL http://www.code666.cn/view/7dd3ed2e
  1. @interface NSArray (SleepSort)
  2. - (void)sleepSortObjectsUsingBlock:(void (^)(id obj))block;
  3. @end
  4.  
  5. @implementation NSArray (SleepSort)
  6.  
  7. - (void)sleepSortObjectsUsingBlock:(void (^)(id obj))block
  8. {
  9.     for (id obj in self) {
  10.         NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:obj, @"obj", block, @"block", nil];
  11.         [self performSelector:@selector(_handleSleepSortItemWithInfo:) withObject:info afterDelay:[obj intValue]];
  12.     }
  13. }
  14.  
  15. - (void)_handleSleepSortItemWithInfo:(NSDictionary *)info
  16. {
  17.     id obj = [info objectForKey:@"obj"];
  18.     void (^block)(id obj) = [info objectForKey:@"block"];
  19.     block(obj);
  20. }
  21.  
  22. @end
  23.  
  24. To use:
  25.  
  26.  
  27.     NSArray *items = [NSArray arrayWithObjects:
  28.         [NSNumber numberWithInt:5],
  29.         [NSNumber numberWithInt:3],
  30.         [NSNumber numberWithInt:6],
  31.         [NSNumber numberWithInt:3],
  32.         [NSNumber numberWithInt:6],
  33.         [NSNumber numberWithInt:3],
  34.         [NSNumber numberWithInt:1],
  35.         [NSNumber numberWithInt:4],
  36.         [NSNumber numberWithInt:7],
  37.         nil];
  38.    
  39.     [items sleepSortObjectsUsingBlock:^(id obj) { NSLog(@"obj = %@", obj); }];
  40. //objectc/6503

回复 "睡眠排序法-objective C版"

这儿你可以回复上面这条便签

captcha