gpt4 book ai didi

objective-c - 为什么 NSMutableArray 没有排序?

转载 作者:行者123 更新时间:2023-12-03 17:28:20 26 4
gpt4 key购买 nike

NSFileManager *fileManager= [[NSFileManager alloc]init];
NSDirectoryEnumerator *myEnumerator= [fileManager enumeratorAtPath:[[theFolder URLByDeletingLastPathComponent]path]];
int f,size=0;
NSMutableArray *dirList=[[NSMutableArray alloc]init];
NSString *fpath;
while (fpath=[myEnumerator nextObject])
{
[dirList addObject:fpath];

}
[dirList sortedArrayUsingSelector:@selector(localizedStandardCompare:)];

dirList 包含类似“name_0012345.tif”的文件名。尽管进行了排序,但数组并不按照我在查找器中看到的按名称排序的顺序包含文件。

最佳答案

-sortedArrayUsingSelector: 实际上是一个 NSArray 方法,它返回一个排序数组(您会立即忽略它)。您的意思是使用 -sortUsingSelector:,这是一个 NSMutableArray 方法,可以重新排列现有数组本身。

在 Cocoa 中,有一种方法用于返回对象的修改后的不可变版本,另一种方法用于修改可变对象本身(-stringByAppendingString:-appendString:,例如)。

关于objective-c - 为什么 NSMutableArray 没有排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18378672/

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