NSFetchRequest propertiesToGroup可以不区分大小写吗?

iiF*_*man 5 grouping core-data objective-c nsfetchrequest

我有以下代码来获取和分组搜索结果:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Song"];
[request setPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:predicates]];
[request setResultType:NSDictionaryResultType];
[request setSortDescriptors: @[[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES]]];

NSExpression *countExpression = [NSExpression expressionWithFormat:@"count:(SELF)"];
NSExpressionDescription *expressionDescriprion = [[NSExpressionDescription alloc] init];
[expressionDescriprion setName:@"count"];
[expressionDescriprion setExpression:countExpression];
[expressionDescriprion setExpressionResultType:NSInteger64AttributeType];

NSArray *properties = @[@"artistName", @"songName"];
[request setPropertiesToFetch:[properties arrayByAddingObject:expressionDescriprion]];
[request setPropertiesToGroupBy:properties];

self.fetchedItems = [self.managedObjectContext executeFetchRequest:request error:nil];
Run Code Online (Sandbox Code Playgroud)

哪个工作非常好但是我遇到了问题并且有点被困,所以我需要以某种方式使这个propertiesToGroupBy(属性类型是NSString*)不区分大小写.目前我有以下输出:

<_PFArray 0x7fa3e3ed0d90>(
{
    artistName = "System of a Down";
    count = 44;
    songName = "Lonely Day";
},
{
    artistName = "System Of A Down";
    count = 2;
    songName = "Lonely Day";
},
{
    artistName = "System of a Down";
    count = 4;
    songName = "Chop Suey";
}
)
Run Code Online (Sandbox Code Playgroud)

这是不正确的,所以我需要前两个项目在单个部分,因为这是相同的艺术家.

有没有办法实现这一目标?

Bor*_*kyi 0

尝试使用 NSExpressionDescription:

NSExpression *artistKeyPathExpression = [NSExpression expressionForKeyPath:@"artistName"];
NSExpression *artistExpression = [NSExpression expressionForFunction:@"uppercase:" arguments:@[artistKeyPathExpression]];
NSExpressionDescription *artistExpressionDescription = [NSExpressionDescription new];
artistExpressionDescription.name = @"groupByArtist";
artistExpressionDescription.expression = artistExpression;
artistExpressionDescription.expressionResultType = NSStringAttributeType;

NSExpression *songKeyPathExpression = [NSExpression expressionForKeyPath:@"songName"];
NSExpression *songExpression = [NSExpression expressionForFunction:@"uppercase:" arguments:@[songKeyPathExpression]];
NSExpressionDescription *songExpressionDescription = [NSExpressionDescription new];
songExpressionDescription.name = @"groupBySongName";
songExpressionDescription.expression = songExpression;
songExpressionDescription.expressionResultType = NSStringAttributeType;

[request setPropertiesToGroupBy:@[artistExpressionDescription, songExpressionDescription]];
Run Code Online (Sandbox Code Playgroud)

请注意,我现在无法在 XCode 中检查此代码,因此它可能包含打印错误。抱歉,但我认为要点很清楚。