2016-02-24 12 views
5

Mam następujący kod do pobierania i grupowanie wyników wyszukiwania:Czy NSFetchRequest propertiesToGroupBy może być niewrażliwy na wielkość liter?

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]; 

który działa bardzo ładne, ale wychodził do wydawania i rodzaj stucked z tak muszę zrobić to propertiesToGroupBy (typu nieruchomość jest NSString *) niewrażliwe na wielkość liter. W tej chwili Mam następujący wynik:

<_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"; 
} 
) 

która jest nieprawidłowa, więc muszę pierwsze 2 pozycje być w jednej sekcji, bo to ten sam artysta.

Czy jest jakiś sposób, aby to osiągnąć?

Odpowiedz

0

Spróbuj użyć 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]]; 

Proszę pamiętać, że nie mogę sprawdzić ten kod teraz w Xcode, więc może zawierać błędy w druku. Przepraszam za to, ale myślę, że główny punkt jest jasny.

+0

To wydaje się niemożliwe do grupowania według NSExpressionDescription dla mnie. Po [request setPropertiesToGroupBy: myExpressions]; Zawsze mam "NSInvalidArgumentException", powód: "Nieprawidłowe wyrażenie keypath – iiFreeman

Powiązane problemy