![](/img/trans.png)
[英]IBOutlet are nil in Objective C before the view controller is presented
[英]Sorting JSON Response before show in table view controller - Objective C
Rewards = (
{
"Restaurant_ID" = 34;
"Rewarded_Points" = 40;
"Rewarded_Time" = "2015-11-23 09:16:00";
},
{
"Restaurant_ID" = 40;
"Rewarded_Points" = 60;
"Rewarded_Time" = "2015-11-24 12:22:56";
},
{
"Restaurant_ID" = 34;
"Rewarded_Points" = 40;
"Rewarded_Time" = "2015-11-24 12:22:56";
}
);
我檢索上面對變量的響應,並使用for循環顯示在TableViewController中
for(int i=0;i<rewards.count;i++){
NSDictionary * rewardsObj = [rewards objectAtIndex:i];
restId = [rewardsObj objectForKey:@"Restaurant_ID"];
rewardedPoints = [rewardsObj objectForKey:@"Rewarded_Points"];
}
[self.tableView reloadData];
當前輸出一次又一次顯示相同的餐廳ID。 我想如果有匹配的ID,則這些點需要計算總和。
34 - Total Points : 40
40 - Total Points : 60
34 - Total Points : 40
//
但是我想像下面這樣排序
34 - Total Points : 80
// 40 + 40
40 - Total Points : 60
使用NSSortDescriptor
作為您的概念,
第1步
首先根據sort選擇鍵,對於我們取的Rewarded_Points
, ascending
排序。
NSSortDescriptor * Rewarded_Points =
[[NSSortDescriptor alloc] initWithKey:@"Rewarded_Points"
ascending:YES];
NSArray *descriptors = [NSArray arrayWithObjects:Rewarded_Points, nil];
NSArray *sortedArrayOfDictionaries = [Rewards sortedArrayUsingDescriptors:descriptors]; // Rewards -> array of dictionary name
NSLog(@"final Value : %@", sortedArrayOfDictionaries);
有關其他教程
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.