iOS - Базовые данные: стандартное отклонение - PullRequest
1 голос
/ 26 марта 2012

Я использовал метод NSExpression expressionForFunction: arguments: с успехом для среднего, суммы, минимума, максимума, количества, с тем же кодом, что и ниже (приведенный в документации Apple).

Но когда речь идет о стандартном отклонении (stddev :) или медиане (медиана :). У меня нет ошибок при компиляции, но я просто получаю сигнал "SIGABRT" в строке executeFetchRequest.

Я не могу определить разницу между средним, суммой, минимумом, максимумом, количеством и стандартным значением или медианой в моем коде. Класс NSExpression не делает различий между параметрами и их возвращаемым объектом, только различие заключается в доступности (Mac OS 10.4 для тех, кто работает, 10.5 для других.

Я использую Xcode 4.2, и мое приложение предназначено для iOS 5 с ARC.

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"myEntity" inManagedObjectContext:app.managedObjectContext];
[request setEntity:entity];
[request setPredicate:[NSPredicate predicateWithFormat:@"something == %d",2]];

// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];

// Create an expression for the key path.
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"anAttribute"];

// Create an expression to represent the minimum value at the key path
NSExpression *statExpression = [NSExpression expressionForFunction:@"stddev:" arguments:[NSArray arrayWithObject:keyPathExpression]];

// Create an expression description using the statExpression and returning a float.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

// The name is the key that will be used in the dictionary for the return value.
[expressionDescription setName:@"statData"];
[expressionDescription setExpression:statExpression];
[expressionDescription setExpressionResultType:NSFloatAttributeType];

// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute the fetch.
NSError *error = nil;
NSArray *objects = [app.managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
    // Handle the error.
}
float result = [[[objects objectAtIndex:0] valueForKey:@"statData"] floatValue];

Спасибо за вашу помощь.

...