【发布时间】:2021-02-17 16:14:37
【问题描述】:
以下是我收到的响应数组。我必须在UITableView 上显示此数据,其中标题将是我的transactionDate,与该日期相关的项目将作为该部分中的行数。
对于下面的代码,我已成功从给定列表中提取出唯一日期,并且在唯一日期数组中获得了 3 个元素。
现在我想要另一个数组,其中包含与唯一日期数组中的日期相关联的项目。
所以结果我应该得到 3 个数组作为 transactionDate 值。
它应该像 array1 有 2 个与日期相关的值对象01/02/2021 array2 将有 1 个与日期相关的值 27/01/2021 和 array3 将有 1 个与日期相关的值对象25/01/2021
随着给定日期的交易继续进行,此数据将继续增加,因此需要一些动态实施。
我的所有数据元素都在数组activityListArray 中,其中包含所有这些元素。
其中我过滤掉了唯一的日期如下,
for i in activityListArray{
date_array.append(i.transactionDate!)
}
let unique = Array(Set(date_array))
现在要获得预期的结果,接下来我应该做什么......?
提前致谢。
我得到的回应如下,
{
accountNumber = 0000078;
amount = 0;
buyingAmount = "277.42";
buyingCurrency = EUR;
currency = "";
customerInstruction = "0201000009958346-000000218";
payeeName = "Ash Roy";
reasonCode = "";
sellingAmount = "254.00";
sellingCurrency = GBP;
status = AWAITINGBANKAPPROVAL;
subType = "Payment without Fx";
transactionDate = "01/02/2021";
type = "";
},
{
amount = 0;
buyingAmount = "436.39";
buyingCurrency = GBP;
currency = "";
customerInstruction = "0201000009958346-000000210";
reasonCode = "";
sellingAmount = "788.00";
sellingCurrency = CAD;
status = PENDING;
subType = "Payment without Fx";
transactionDate = "27/01/2021";
type = "";
},
{
amount = 0;
buyingAmount = "436.39";
buyingCurrency = GBP;
currency = "";
customerInstruction = "0201000009958346-000000207";
reasonCode = "";
sellingAmount = "788.00";
sellingCurrency = CAD;
status = PENDING;
subType = "Payment without Fx";
transactionDate = "25/01/2021";
type = "";
},
{
amount = 0;
buyingAmount = "436.39";
buyingCurrency = GBP;
currency = "";
customerInstruction = "0201000009958346-000000206";
reasonCode = "";
sellingAmount = "788.00";
sellingCurrency = CAD;
status = PENDING;
subType = "Payment without Fx";
transactionDate = "01/02/2021";
type = "";
}
【问题讨论】:
-
您可以使用
Dictionary(grouping:by)这样做。