我正在尝试从数组totalPurchaseSum
中的所有对象获取属性stockPurchasePrice
的总值arrPurchaseActions
。
这是属性(user.h):
@property (nonatomic, strong) NSMutableArray *arrPurchaseActions;
@property (nonatomic) CGFloat totalSumPurchased;
-(CGFloat)totalSumPurchased:(CGFloat)stockPurchasePrice;
这是我尝试使用的方法(User.m):
-(CGFloat)totalSumPurchased:(CGFloat)stockPurchasePrice
{
CGFloat totalSumPurchased = 0;
size_t count = [self.arrPurchaseActions count];
for (int i = 0; i < count; i++) {
totalSumPurchased = totalSumPurchased + [[self.arrPurchaseActions[i].stockPurchasePrice];
}
return totalSumPurchased;
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
试试这个:
NSNumber * sum = [self.arrPurchaseActions valueForKeyPath:@"@sum.stockPurchasePrice"];
答案 1 :(得分:0)
这一行:
totalSumPurchased = totalSumPurchased + [[self.arrPurchaseActions[i].stockPurchasePrice];
需要:
totalSumPurchased = totalSumPurchased + [[self.arrPurchaseActions[i] stockPurchasePrice];
您不能将属性访问器与id
类型的对象一起使用(NSArray objectAtIndex:
返回的对象)。相反,请为属性调用getter方法。