Ios 启用ARC时,NSComparator在对NSArray排序时更改行为
我正在使用NSComparator对NSArray中的对象进行排序。当代码未启用ARC时,比较器在代码启用ARC时给出不同的结果 以下是我的代码片段: - (NSArray *)sortedToolsInfoArrayWithKey { NSArray *aReturnVal = nil; NSArray *aToolsInfoArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ToolsData" ofType:@"plist"]];Ios 启用ARC时,NSComparator在对NSArray排序时更改行为,ios,objective-c,sorting,automatic-ref-counting,comparator,Ios,Objective C,Sorting,Automatic Ref Counting,Comparator,我正在使用NSComparator对NSArray中的对象进行排序。当代码未启用ARC时,比较器在代码启用ARC时给出不同的结果 以下是我的代码片段: - (NSArray *)sortedToolsInfoArrayWithKey { NSArray *aReturnVal = nil; NSArray *aToolsInfoArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathFo
aReturnVal = [aToolsInfoArray sortedArrayUsingComparator:(NSComparator)^(NSDictionary *a, NSDictionary *b) {
[[a valueForKey:@"selectionCount"] compare:[b valueForKey:@"selectionCount"]];
}];
[aToolsInfoArray release];
return aReturnVal;
}这个方法是用非ARC代码编写的,我需要相同类型的排序数组,请注意,我的要求是我需要将从同一pList文件中拾取的相同数组排序到两个不同的文件中,其中一个文件启用了ARC,而另一个文件未启用ARC。 但当我这样做时,我得到的排序顺序正好相反,当我禁用ARC时,问题就解决了 我无法理解NSComparator在ARC和非ARC启用文件中排序数组的不同行为背后的逻辑
谢谢..您不必
从比较器块返回值:
aReturnVal = [aToolsInfoArray sortedArrayUsingComparator:^(id a, id b) {
NSDictionary *adict = (NSDictionary *)a;
NSDictionary *bdict = (NSDictionary *)b;
return [[adict objectForKey:@"selectionCount"] compare:[bdict objectForKey:@"selectionCount"]];
}];
(加上使用objectForKey
而不是valueForKey
,后者是KVC访问器)
编辑遗漏了您使用的块语法也不正确(多亏了@Ivan Genchev)。您没有从比较器块返回值:
aReturnVal = [aToolsInfoArray sortedArrayUsingComparator:^(id a, id b) {
NSDictionary *adict = (NSDictionary *)a;
NSDictionary *bdict = (NSDictionary *)b;
return [[adict objectForKey:@"selectionCount"] compare:[bdict objectForKey:@"selectionCount"]];
}];
(加上使用objectForKey
而不是valueForKey
,后者是KVC访问器)
编辑遗漏了您使用的块语法也不正确(感谢@Ivan Genchev)。您还可以使用NSSortDescriptor
对数组进行排序
NSArray *testArray = @[@{@"selectionCount":@"3"},
@{@"selectionCount":@"1"},
@{@"selectionCount":@"7"},
@{@"selectionCount":@"2"}];
NSLog(@"testArray: %@", testArray);
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"selectionCount" ascending:YES];
NSArray *sortedTestArray = [testArray sortedArrayUsingDescriptors:@[sortDescriptor]];
NSLog(@"sortedTestArray: %@", sortedTestArray);
您还可以使用NSSortDescriptor
对数组进行排序
NSArray *testArray = @[@{@"selectionCount":@"3"},
@{@"selectionCount":@"1"},
@{@"selectionCount":@"7"},
@{@"selectionCount":@"2"}];
NSLog(@"testArray: %@", testArray);
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"selectionCount" ascending:YES];
NSArray *sortedTestArray = [testArray sortedArrayUsingDescriptors:@[sortDescriptor]];
NSLog(@"sortedTestArray: %@", sortedTestArray);
作为“trojanfoe”回答的后续内容,该块的语法错误。你需要:
aReturnVal = [aToolsInfoArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
return [[a valueForKey:@"selectionCount"] compare:[b valueForKey:@"selectionCount"]];
}];
让Xcode的代码完成为您完成所有这些。它避免了您所犯的那种错误。作为“特洛伊敌人”回答的后续内容,您的块语法错误。你需要:
aReturnVal = [aToolsInfoArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
return [[a valueForKey:@"selectionCount"] compare:[b valueForKey:@"selectionCount"]];
}];
让Xcode的代码完成为您完成所有这些。它避免了您所犯的错误。用plist文件的一些条目更新您的问题,这样我们就可以看到您正在使用的内容。用plist文件的一些条目更新您的问题,这样我们就可以看到您正在使用的内容。很好地掌握了return
。但是缺少返回值不会导致编译器错误吗?@rmaddy是的,我希望是这样。我假设OP已经发布了他的实际代码,但是…只是尝试了一下,它没有引起错误,甚至没有引起警告,因为他正在将块投射到NSComparator
@rmaddy请参见NSObjCRuntime.h
和typedef nscomparationresult(^NSComparator)(id obj1,id obj2)代码>特别好。@IvanGenchev捕捉得很好。错过了。很好地抓住了返回的。但是缺少返回值不会导致编译器错误吗?@rmaddy是的,我希望是这样。我假设OP已经发布了他的实际代码,但是…只是尝试了一下,它没有引起错误,甚至没有引起警告,因为他正在将块投射到NSComparator
@rmaddy请参见NSObjCRuntime.h
和typedef nscomparationresult(^NSComparator)(id obj1,id obj2)代码>特别好。@IvanGenchev捕捉得很好。错过了。