gpt4 book ai didi

objective-c - sortUsingSelector 不对 NSStrings 数组进行排序

转载 作者:太空狗 更新时间:2023-10-30 03:59:16 26 4
gpt4 key购买 nike

这让我很困惑。我有一个函数可以执行此操作:

void ListAllStoredLocations(NSString *SearchTerm){  
NSMutableDictionary *item;
NSString* filePath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/Preferences/yourprogram.plist"];

item = [[[NSMutableDictionary alloc] initWithContentsOfFile:filePath] mutableCopy];

NSMutableArray *ReadStoredArray = [item objectForKey:SearchTerm];
NSMutableArray *SortedArray = [[NSMutableArray alloc] init];
NSString *CurrentResult=@"";

for (int i = 0; i< [ReadStoredArray count]; i++){
CurrentResult=(NSString *)[ReadStoredArray objectAtIndex:i];
[SortedArray addObject:CurrentResult];
}

[SortedArray sortUsingSelector:@selector(compare:)];

for (int i = 0; i< [SortedArray count]; i++){
NSLog(@"%@",[SortedArray objectAtIndex:i]);
}


[item release];

它在第一个 for 循环中找到输出 NSStrings,如下所示:

Location1

Location2

Not a location

Location2

Location3

Location2

我希望输出按字母顺序排列:

Location1

Location2

Location2

Location2

Location3

Not a location

但是,无论如何,“[SortedArray sortUsingSelector:@selector(compare:)];”只是不对数组进行排序。没有任何反应。

也许我的做法全错了,但我在网上看到的每个示例都像这样对 NSString 进行排序 - 所以我不知道该怎么做。

我的结局,如果有更好的解决方案,那就是输出最大重复条目的计数。我当时认为排序将是朝着这个方向迈出的一步。

真的,我正在寻找的是输出:

Location2

因为“location2”在该列表中重复次数最多。

有什么帮助吗?

最佳答案

鉴于您的字符串数组如下所示:

NSMutableArray * array = [NSMutableArray array];
[array addObject:@"Location1"];
[array addObject:@"Location2"];
[array addObject:@"Not a location"];
[array addObject:@"Location2"];
[array addObject:@"Location3"];
[array addObject:@"Location2"];

NSLog(@"------------- original:");
for (id obj in array) NSLog(@"%@", obj);

你可以这样排序:

NSLog(@"------------- sorted:");
NSArray * sortedArray =
[array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (id obj in sortedArray) NSLog(@"%@", obj);

输出:

2010-02-06 00:24:14.915 x[23867:903] ------------- original:
2010-02-06 00:24:14.917 x[23867:903] Location1
2010-02-06 00:24:14.921 x[23867:903] Location2
2010-02-06 00:24:14.922 x[23867:903] Not a location
2010-02-06 00:24:14.922 x[23867:903] Location2
2010-02-06 00:24:14.923 x[23867:903] Location3
2010-02-06 00:24:14.924 x[23867:903] Location2
2010-02-06 00:24:14.924 x[23867:903] ------------- sorted:
2010-02-06 00:24:14.925 x[23867:903] Location1
2010-02-06 00:24:14.926 x[23867:903] Location2
2010-02-06 00:24:14.926 x[23867:903] Location2
2010-02-06 00:24:14.927 x[23867:903] Location2
2010-02-06 00:24:14.927 x[23867:903] Location3
2010-02-06 00:24:14.928 x[23867:903] Not a location

如果要查找出现次数最多的对象,给定原始数组:

NSCountedSet * set = [[NSCountedSet alloc] initWithArray:array];
for (id obj in set) NSLog(@"%d - %@", [set countForObject:obj], obj);

int count = 0;
int maxc = 0;
id maxobj;
for (id obj in set)
{
count = [set countForObject:obj];
if (maxc < count) maxc = count, maxobj = obj;
}

NSLog(@"max is: %d - %@", maxc, maxobj);

输出:

2010-02-06 00:39:46.310 x[24516:903] 1 - Location1
2010-02-06 00:39:46.311 x[24516:903] 1 - Not a location
2010-02-06 00:39:46.311 x[24516:903] 3 - Location2
2010-02-06 00:39:46.312 x[24516:903] 1 - Location3
2010-02-06 00:39:46.313 x[24516:903] max is: 3 - Location2

关于objective-c - sortUsingSelector 不对 NSStrings 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2205332/

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