试图获取数组中属性的总和

时间:2013-09-20 20:44:21

标签: ios core-data properties nsarray sum

我有一个NSArray对象,数据看起来像这样。我目前正在尝试总结给定任务的timeSheets属性中的小时值。我目前有一个双循环功能,可以为iPhone完成工作,但是在iPad版本的UITableView上出现滞后性能问题。 double for循环的代码位于底部。我正在尝试使用NSExpressions和NSPredicates的混合物,但我似乎无法正确添加这些。

     <Tasks: 0xc563950> (entity: Tasks; id: 0xc561940 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p8> ; data: {
    assigneeID = 20;
    budgetedHours = 5;
    chargeNumber = 0;
    completedProgress = 0;
    createdBy = "Tyler Baetz";
    dateCreated = "2013-09-06 19:18:40 +0000";
    dateModified = "2013-09-11 04:53:32 +0000";
    descriptions = nil;
    dueDate = "2013-09-11 07:00:00 +0000";
    modifiedBy = "Jose Alvarez";
    name = Denver;
    ownerID = 120;
    parentID = 1914;
    project = "0xc566070 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Projects/p2>";
    projectID = 500910;
    taskID = 1916;
    taskPriority = nil;
    taskPriorityID = 1;
    taskStatus = nil;
    taskStatusID = 3;
    taskType = nil;
    taskTypeID = 4;
    tasks = "<relationship fault: 0xb4e9db0 'tasks'>";
    timeSheets =     (
        "0xc553780 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p12>",
        "0xc5537a0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p14>",
        "0xc5537b0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p15>"
    );
}) with objects {(
    <TimeSheets: 0xc555420> (entity: TimeSheets; id: 0xc553780 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p12> ; data: {
    approvedBy = nil;
    comments = nil;
    dateApproved = nil;
    dateEntered = "2013-09-06 20:31:31 +0000";
    employee = nil;
    employeeID = 20;
    enteredBy = 20;
    hours = 173;
    reportPeriod = nil;
    reportPeriodID = 4;
    task = "0xc561940 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p8>";
    taskID = 1916;
    timeSheetDate = "2013-09-01 07:00:00 +0000";
    timeSheetID = 1934;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc555730> (entity: TimeSheets; id: 0xc5537a0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p14> ; data: {
    approvedBy = nil;
    comments = "Friday :45";
    dateApproved = nil;
    dateEntered = "2013-09-06 20:31:31 +0000";
    employee = nil;
    employeeID = 20;
    enteredBy = 20;
    hours = 45;
    reportPeriod = nil;
    reportPeriodID = 4;
    task = "0xc561940 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p8>";
    taskID = 1916;
    timeSheetDate = "2013-09-06 07:00:00 +0000";
    timeSheetID = 1937;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc5557a0> (entity: TimeSheets; id: 0xc5537b0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p15> ; data: {
    approvedBy = nil;
    comments = "Hello Man";
    dateApproved = nil;
    dateEntered = "2013-09-11 13:20:19 +0000";
    employee = nil;
    employeeID = 20;
    enteredBy = 50;
    hours = 300;
    reportPeriod = nil;
    reportPeriodID = 4;
    task = "0xc561940 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p8>";
    taskID = 1916;
    timeSheetDate = "2013-09-05 07:00:00 +0000";
    timeSheetID = 2357;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
})
)},
Relationship 'timeSheets' on managed object (0xc5639f0) <Tasks: 0xc5639f0> (entity: Tasks; id: 0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9> ; data: {
    assigneeID = 20;
    budgetedHours = 3;
    chargeNumber = 0;
    completedProgress = 0;
    createdBy = "Tyler Baetz";
    dateCreated = "2013-09-06 19:17:01 +0000";
    dateModified = "2013-09-12 18:34:55 +0000";
    descriptions = nil;
    dueDate = "2013-09-09 07:00:00 +0000";
    modifiedBy = "Jose Alvarez";
    name = "Jose's Task";
    ownerID = 120;
    parentID = 1913;
    project = "0xc566070 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Projects/p2>";
    projectID = 500910;
    taskID = 1915;
    taskPriority = nil;
    taskPriorityID = 4;
    taskStatus = nil;
    taskStatusID = 3;
    taskType = nil;
    taskTypeID = 4;
    tasks = "<relationship fault: 0xb4ee0d0 'tasks'>";
    timeSheets =     (
        "0xc5537c0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p16>",
        "0xc553810 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p21>",
        "0xc553800 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p20>",
        "0xc553790 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p13>",
        "0xc5537d0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p17>"
    );
}) with objects {(
    <TimeSheets: 0xc5557f0> (entity: TimeSheets; id: 0xc5537c0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p16> ; data: {
    approvedBy = nil;
    comments = nil;
    dateApproved = nil;
    dateEntered = "2013-09-11 17:05:11 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 205;
    reportPeriod = nil;
    reportPeriodID = 5;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-11 17:05:18 +0000";
    timeSheetID = 2633;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc5559b0> (entity: TimeSheets; id: 0xc553810 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p21> ; data: {
    approvedBy = nil;
    comments = Rarer;
    dateApproved = nil;
    dateEntered = "2013-09-17 22:05:11 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 0;
    reportPeriod = nil;
    reportPeriodID = 6;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-19 07:00:00 +0000";
    timeSheetID = 3297;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc555950> (entity: TimeSheets; id: 0xc553800 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p20> ; data: {
    approvedBy = nil;
    comments = Cbfb;
    dateApproved = nil;
    dateEntered = "2013-09-17 22:33:05 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 0;
    reportPeriod = nil;
    reportPeriodID = 6;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-16 07:00:00 +0000";
    timeSheetID = 3298;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc5556c0> (entity: TimeSheets; id: 0xc553790 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p13> ; data: {
    approvedBy = nil;
    comments = nil;
    dateApproved = nil;
    dateEntered = "2013-09-12 18:34:55 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 284;
    reportPeriod = nil;
    reportPeriodID = 5;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-12 18:21:59 +0000";
    timeSheetID = 2827;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
}),
    <TimeSheets: 0xc555850> (entity: TimeSheets; id: 0xc5537d0 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/TimeSheets/p17> ; data: {
    approvedBy = nil;
    comments = Asdad;
    dateApproved = nil;
    dateEntered = "2013-09-17 22:03:32 +0000";
    employee = "0xb4b8660 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Employees/p23>";
    employeeID = 20;
    enteredBy = 20;
    hours = 0;
    reportPeriod = nil;
    reportPeriodID = 6;
    task = "0xc561950 <x-coredata://36042CA3-F392-46FB-AF23-F0C396CE0989/Tasks/p9>";
    taskID = 1915;
    timeSheetDate = "2013-09-15 07:00:00 +0000";
    timeSheetID = 3296;
    timeStatus = nil;
    timeStatusID = 1;
    timeType = nil;
    timeTypeID = 35;
})
)}
)

Double For循环代码:

- (NSNumber *) getSumOfChargedHoursForAnArrayOfTasks: (NSArray *) tasksArray
{
    NSArray *timeSheets = [tasksArray valueForKeyPath:@"timeSheets"];

    NSNumber *chargedHours = [NSNumber numberWithInt:0];

    for(int i = 0; i < [timeSheets count]; ++i)
    {
        NSArray *innerTimeSheet = [timeSheets objectAtIndex:i];

        for(int j = 0; j < [innerTimeSheet count]; ++j)
        {
            NSNumber *innerNumber = [innerTimeSheet valueForKeyPath:@"@sum.hours"];

            chargedHours = [NSNumber numberWithFloat:[chargedHours intValue] + [innerNumber intValue]];
        }

    }

    return chargedHours;
}

1 个答案:

答案 0 :(得分:2)

如果您尝试将一系列任务中的所有时间表中的小时数相加,那么您应该考虑以下列方式使用valueForKeyPath:

[tasks valueForKeyPath:@"@sum.timeSheets.@sum.hours"]

现在看来,您将每个任务的timeSheets小时数乘以每个任务的时间表数。