gpt4 book ai didi

nsmutablearray - 对 NSIndexPaths 数组进行排序

转载 作者:行者123 更新时间:2023-12-04 00:05:20 28 4
gpt4 key购买 nike

我有一个 NSMutableArray包含 NSIndexPath对象,我想按它们的 row 对它们进行排序, 升序。

最短/最简单的方法是什么?

这是我尝试过的:

[self.selectedIndexPaths sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSIndexPath *indexPath1 = obj1;
NSIndexPath *indexPath2 = obj2;
return [@(indexPath1.section) compare:@(indexPath2.section)];
}];

最佳答案

你说你想按 row 排序,但你比较section .此外,sectionNSInteger ,所以你不能调用它的方法。

如下修改您的代码以对 row 进行排序:

[self.selectedIndexPaths sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSInteger r1 = [obj1 row];
NSInteger r2 = [obj2 row];
if (r1 > r2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (r1 < r2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];

关于nsmutablearray - 对 NSIndexPaths 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14928793/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com