Sim*_*e99 3 macos objective-c nssortdescriptor ios
我有一个switch
创造相关的声明NSSortDescriptor
.对于一些NSSortDescriptors
我使用的block
作为自定义comparator
(比较CMTimes
).以下代码工作正常,但我想添加一些NSSortDescriptors
比较CMTimes
.由于block
总是相同的是可以创建一个variable
来保持,block
所以我不需要继续复制和粘贴杂乱的代码.我想它应该是可能的,但我似乎无法让它发挥作用.我非常感谢任何帮助.谢谢!
NSSortDescriptor *sortDescriptor;
switch (mode) {
case 1:
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:^(id first, id second){
CMTime time1 = [first CMTimeValue];
CMTime time2 = [second CMTimeValue];
if (CMTIME_COMPARE_INLINE(time1, <, time2))
return NSOrderedAscending;
else if (CMTIME_COMPARE_INLINE(time1, >, time2))
return NSOrderedDescending;
else
return NSOrderedSame;
}];
break;
case 2:
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: NO comparator:^(id first, id second){
CMTime time1 = [first CMTimeValue];
CMTime time2 = [second CMTimeValue];
if (CMTIME_COMPARE_INLINE(time1, <, time2))
return NSOrderedAscending;
else if (CMTIME_COMPARE_INLINE(time1, >, time2))
return NSOrderedDescending;
else
return NSOrderedSame;
}];
break;
case 3:
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"info" ascending: YES];
break;
case 4:
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"info" ascending: NO];
break;
default:
break;
}
Run Code Online (Sandbox Code Playgroud)
您可以创建块变量,这样就不必复制和粘贴块代码.
NSComparator comparisonBlock = ^(id first,id second) {
return NSOrderedAscending;
};
[NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:comparisonBlock];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5106 次 |
最近记录: |