Find the average of an NSMutableArray with valueForKeyPath

后端 未结 2 1985
清酒与你
清酒与你 2021-01-13 09:47

Currently I\'m trying to find a compact way to average a matrix. The obvious solution is to sum the matrix, then divide by the number of elements. I have, h

2条回答
  •  走了就别回头了
    2021-01-13 10:03

    No, you can't do like this. The object Transaction is a Modal Class. This class is having three properties, namely

    • payee
    • amount
    • date

    Each row in this image represents one Transaction modal object.

    enter image description here

    transactions is an array which is holding all these rows (Transaction Modal Objects).

    In these transactions array, they are trying to calculate the Transaction Modal amount field average using the operator @avg. So, its like

    NSNumber *transactionAverage = [transactions valueForKeyPath:@"@avg.amount"];
    

    your array doesn't have the key self. So that's the problem

提交回复
热议问题