我正在构建一个个人预算应用程序来跟踪我的购买。我的NSDictionary
为NSArray
,如下所示:
{
46 = (
{
DATE = "11/14/2019";
ID = 21;
PLACE = Kroger;
PRICE = "23.32";
},
{
DATE = "11/14/2019";
ID = 22;
PLACE = Walmart;
PRICE = "12.54";
}
);
47 = (
{
DATE = "11/18/2019";
ID = 23;
PLACE = Cinco;
PRICE = "65.32";
}
);
}
NSDictionary
中的键由strftime
使用%W
确定。基本上,我有一个每周可以使用的上限(即190),并且我希望每个titleForHeaderInSection:
的总值由每个键中每个字段的PRICE
确定。我目前在应用程序上具有以下内容:
以下将计算上面列出的NSDictionary
以及总值:注意:arrFoodInfo是just项的NSDictionary
,没有星期数
for (NSDictionary *dict in self.arrFoodInfo) {
NSString *date = [dict objectForKey:@"DATE"];
[totalPrices addObject:[dict objectForKey:@"PRICE"]];
for (NSString *price in totalPrices) {
if ([totalPrices containsObject:price]) {
_sumPrices += [price floatValue]; // Calculates total value
[totalPrices removeObject:price];
}
}
NSDateFormatter *SDF = [[NSDateFormatter alloc]
init];
[SDF setDateStyle:NSDateFormatterMediumStyle];
[SDF setDateFormat:@"MM/dd/yyyy"];
NSDate *convertedDate = [SDF dateFromString:date];
NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitWeekOfYear fromDate:convertedDate];
NSInteger week = dateComponents.weekOfYear;
NSInteger index = week;
NSNumber *key = @(index);
NSMutableArray *weekArray = weeksDictionary[key];
if (!weekArray) {
weekArray = [NSMutableArray array];
weeksDictionary[key] = weekArray;
}
[weekArray addObject:dict];
}
目前,我得到每个PRICE
的总和,但每个部分的总价值应该有所不同。确定此的最佳选择是什么?
请在标头部分中显示您的代码以计算总价。
如果您对数据没有特殊要求,我建议您更改为here之类的结构。如果遵循此结构,则可以轻松处理每个节以及每个节的行的数据。