Objective c NSSortDescriptors中的块-目标C
我有一个Objective c NSSortDescriptors中的块-目标C,objective-c,ios,macos,nssortdescriptor,Objective C,Ios,Macos,Nssortdescriptor,我有一个switch语句,它创建了一个相关的NSSortDescriptor。对于一些nssortddescriptor我使用块作为自定义比较器(用于比较CMTimes)。下面的代码工作正常,但我想添加更多的nssortddescriptor,同时比较CMTimes。由于块总是相同的,所以可以创建一个变量来保存块,这样我就不需要一直复制和粘贴凌乱的代码。我想这应该是可能的,但我似乎无法让它发挥作用。我将非常感谢任何帮助。谢谢大家! NSSortDescriptor *sortDescriptor
switch
语句,它创建了一个相关的NSSortDescriptor
。对于一些nssortddescriptor
我使用块作为自定义比较器
(用于比较CMTimes
)。下面的代码工作正常,但我想添加更多的nssortddescriptor
,同时比较CMTimes
。由于块
总是相同的,所以可以创建一个变量
来保存块
,这样我就不需要一直复制和粘贴凌乱的代码。我想这应该是可能的,但我似乎无法让它发挥作用。我将非常感谢任何帮助。谢谢大家!
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;
}
NSSortDescriptor*sortDescriptor;
开关(模式){
案例1:
sortDescriptor=[NSSortDescriptor sortDescriptor WithKey:@“startTime”升序:是比较器:^(id第一,id第二){
CMTime time1=[第一个CMTimeValue];
CMTime time2=[第二个CMTime值];
if(CMTIME\u COMPARE\u INLINE(time1,time2))
退而求其次;
其他的
返回指定名称;
}];
打破
案例2:
sortDescriptor=[NSSortDescriptor sortDescriptor WithKey:@“startTime”升序:无比较器:^(id第一,id第二){
CMTime time1=[第一个CMTimeValue];
CMTime time2=[第二个CMTime值];
if(CMTIME\u COMPARE\u INLINE(time1,time2))
退而求其次;
其他的
返回指定名称;
}];
打破
案例3:
sortDescriptor=[NSSortDescriptor sortDescriptor WithKey:@“info”升序:是];
打破
案例4:
sortDescriptor=[NSSortDescriptor sortDescriptor WithKey:@“info”升序:否];
打破
违约:
打破
}
当然可以,对属性使用@property(非原子,复制)
(不要忘记释放),或者在赋值之前定义一个块。您可以创建一个块变量,这样就不必复制和粘贴块代码
NSComparator comparisonBlock = ^(id first,id second) {
return NSOrderedAscending;
};
[NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:comparisonBlock];
你可以按照
NSComparator myBlock = ^(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;
}
一切都应该很好
希望这有帮助,如果还有什么我可以帮忙的,请告诉我:)谢谢达姆里克。那很好用。我想我现在更了解积木了…嗨,乔治。由于某种原因,我无法让你的解决方案发挥作用。。。特别是<代码>(^myBlock)(id,id)
。如果我只是用上面DHamrick的回答中的myBlock
来替换它,它就可以了。对不起,这完全是我的错。你完全正确,我打错了。
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:myBlock];