NSSortDescriptors 中的块 - Objective C

发布于 2024-12-29 15:35:31 字数 1876 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你手 2025-01-05 15:35:31

您可以创建块变量,这样就不必复制和粘贴块代码。

NSComparator comparisonBlock = ^(id first,id second) {
    return NSOrderedAscending;
};
[NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:comparisonBlock];

You can create a block variable so that you don't have to copy and paste the block code.

NSComparator comparisonBlock = ^(id first,id second) {
    return NSOrderedAscending;
};
[NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:comparisonBlock];
苏佲洛 2025-01-05 15:35:31

您可以执行以下操作,

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,它是一个返回类型为 NSComparator 的块,并采用两个 id 类型参数。

然后您应该能够调用例如:

sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:myBlock];

并且一切都应该运行良好。

希望这对您有所帮助,如果还有其他需要帮助的地方,请告诉我:)

You could do something along the lines of

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

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:

sortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"startTime" ascending: YES comparator:myBlock];

And everything should work nicely.

Hope this helps, let me know if there's anything else I can help with :)

昇り龍 2025-01-05 15:35:31

当然,使用 @property (nonatomic, copy) 作为属性(不要忘记释放),或者只是在赋值之前定义一个块。

Sure, use @property (nonatomic, copy) for property (don't forget to release), or just define a block before your assignments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文