2016-02-24 18 views
5

Tôi đã sau mã cho lấy và nhóm kết quả tìm kiếm:Các thuộc tính NSFetchRequest có thể là Nhóm không phân biệt chữ hoa chữ thường không?

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

mà hoạt động rất đẹp, nhưng tôi phải đối mặt với phát hành và loại stucked với, vì vậy tôi cần phải thực hiện propertiesToGroupBy này (loại tài sản được NSString *) trường hợp không nhạy cảm bằng cách nào đó. Tại thời điểm Tôi đã theo sản lượng:

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

đó là không chính xác, vì vậy tôi cần 2 mục đầu tiên là trong phần duy nhất, gây ra điều này là các nghệ sĩ cùng.

Có cách nào để đạt được điều đó không?

Trả lời

0

Hãy thử sử dụng 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]]; 

Xin vui lòng, lưu ý rằng tôi không thể kiểm tra mã này ngay bây giờ trong XCode, vì vậy nó có thể chứa lỗi in. Xin lỗi về điều đó, nhưng tôi nghĩ điểm chính là rõ ràng.

+0

Điều đó dường như không thể nhóm bởi NSExpressionDescription cho tôi. Sau [yêu cầu setPropertiesToGroupBy: myExpressions]; Tôi luôn có 'NSInvalidArgumentException', lý do: 'Biểu thức phím tắt không hợp lệ – iiFreeman

Các vấn đề liên quan