NSSortDescriptors 中的块 - Objective C
我有一个 switch 语句,它创建了相关的 NSSortDescriptor 。对于某些NSSortDescriptors
,我使用块
作为自定义比较器
(以比较CMTimes
)。以下代码工作正常,但我想添加更多 NSSortDescriptors
来比较 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;
}
I have a switch
statement which creates a relevant NSSortDescriptor
. For some of the NSSortDescriptors
I am using a block
as a custom comparator
(to compare CMTimes
). The following code works fine but I would like to add some more NSSortDescriptors
also comparing CMTimes
. As the block
is always the same is it possible to create a variable
to hold the block
so I don't need to keep copying and pasting messy code. I guess it should be possible but I can't seem to get it to work. I would greatly appreciate any help. Thank you!
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建块变量,这样就不必复制和粘贴块代码。
You can create a block variable so that you don't have to copy and paste the block code.
您可以执行以下操作,
这将为您创建一个变量 myBlock,它是一个返回类型为 NSComparator 的块,并采用两个 id 类型参数。
然后您应该能够调用例如:
并且一切都应该运行良好。
希望这对您有所帮助,如果还有其他需要帮助的地方,请告诉我:)
You could do something along the lines of
This will create you a variable myBlock that is a block with return type NSComparator, and take two id type arguments.
You should then be able to call for example:
And everything should work nicely.
Hope this helps, let me know if there's anything else I can help with :)
当然,使用
@property (nonatomic, copy)
作为属性(不要忘记释放),或者只是在赋值之前定义一个块。Sure, use
@property (nonatomic, copy)
for property (don't forget to release), or just define a block before your assignments.