与此有关系的实体如下:

我想要的是一个艺术家有多少张专辑。所以我想用组。但我不想重复所有的艺术家和计数的专辑。
所以我想出了一个代码:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Album" inManagedObjectContext:objectStore.mainQueueManagedObjectContext];
[fetchRequest setEntity:entity];
NSExpressionDescription* ex = [[NSExpressionDescription alloc] init];
[ex setExpression:[NSExpression expressionWithFormat:@"count:(artist)"]];
[ex setName:@"count"];
[ex setExpressionResultType:NSDecimalAttributeType];
[fetchRequest setPropertiesToFetch:@[ @"artist", ex ]];
[fetchRequest setPropertiesToGroupBy:@[ @"artist" ]];
[fetchRequest setResultType:NSDictionaryResultType ];
id results = [objectStore.mainQueueManagedObjectContext executeFetchRequest:fetchRequest error:nil];返回以下结果:
<_PFArray 0x7fa1f34c4350>(
{
artist = "0xd000000000080000 <x-coredata://969B0BFF-9B87-4280-9B41-7920138902FC/Artist/p2>";
count = "0xd0000000000c0000 <x-coredata://969B0BFF-9B87-4280-9B41-7920138902FC/Artist/p3>";
},
{
artist = "0xd000000000280000 <x-coredata://969B0BFF-9B87-4280-9B41-7920138902FC/Artist/p10>";
count = "0xd000000000080000 <x-coredata://969B0BFF-9B87-4280-9B41-7920138902FC/Artist/p2>";
},
{
artist = "0xd000000000300000 <x-coredata://969B0BFF-9B87-4280-9B41-7920138902FC/Artist/p12>";
count = "0xd000000000380000 <x-coredata://969B0BFF-9B87-4280-9B41-7920138902FC/Artist/p14>";
},
{
artist = "0xd000000000400000 <x-coredata://969B0BFF-9B87-4280-9B41-7920138902FC/Artist/p16>";
count = "0xd000000000040000 <x-coredata://969B0BFF-9B87-4280-9B41-7920138902FC/Artist/p1>";
}
)我以为会有不同的事情。当我使用releaseYear而不是artist时,结果如下(如预期的):
<_PFArray 0x7f9f235e91c0>(
{
count = 2;
releaseYear = 0;
},
{
count = 2;
releaseYear = 1983;
},
{
count = 1;
releaseYear = 1984;
},
{
count = 1;
releaseYear = 1985;
}
)这种提取是否只适用于原始数据类型,而不适用于对象关系?任何帮助都是非常感谢的!谢谢:)
发布于 2016-07-28 09:53:31
正如注释中提到的,使用title解决了这个问题。更新后的表达式如下所示:
NSExpressionDescription* ex = [[NSExpressionDescription alloc] init];
[ex setExpression:[NSExpression expressionWithFormat:@"count:(title)"]];
[ex setName:@"count"];
[ex setExpressionResultType:NSDecimalAttributeType];https://stackoverflow.com/questions/38606230
复制相似问题