0

Following is the array I am receiving in response.
 I have to show this data on UITableView where header title will be my transactionDate and items related to that date will go as number of rows in that section.

For below code, I have successfully pulled out unique dates from given list and I got 3 elements in unique date array.

Now I want another array which will have items associated to the date from unique date array.

So in result I should get 3 arrays for transactionDate value. It should be like array1 with 2 value objects related to date 01/02/2021 array2 will be with 1 value related to date 27/01/2021 and array3 will be with 1 value object related to value to date 25/01/2021

This data will go on increasing as transactions for given date will go on, so some dynamic implementation is needed.

My all data elements are in array activityListArray which contains all these elements. Among which I have filtered out unique dates as below,

for i in activityListArray{
            date_array.append(i.transactionDate!)
        }
        let unique = Array(Set(date_array))

Now to get expected result what should I do next….?

Thanks in advance.

Response I am getting is below,

 {
            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 = "";
        }
1
  • 1
    You can use Dictionary(grouping:by) to do so. Commented Feb 17, 2021 at 16:24

1 Answer 1

1

You dont need to pull out unique dates, Swift standard library has already provided an API to Dictionary, where in you can group the collection of objects based on specific field using init(grouping:by:)

read all about it here https://developer.apple.com/documentation/swift/dictionary/3127163-init

let dict = Dictionary<String,[YourObject]>(grouping: activityListArray, by: {$0.transactionDate})

BTW Dictionary<String,[YourObject]> explicit type declaration is not needed, just realised it, so it can be as simple as

let dict = Dictionary(grouping: activityListArray, by: {$0.transactionDate})

This will return a Dictionary with unique transactionDate as keys and array of objects with that transactionDate as values.

Now you can simply return number of sections as dict.keys.count and number of rows in each section as dict[Array(dict.keys)[indexPath.section]]?.count

Sign up to request clarification or add additional context in comments.

9 Comments

@sagar: Please check the answer posted and lemme know if you are still facing issue, if its working fine please consider accepting the answer
@ Sandeep Bhandari :- Thanks buddy, that helped me a lot and kudos to answer. Thank you very much.
@sagar: glad I could help :)
@ Sandeep Bhandari:- Just quick update help needed, can we sort the data using transactionDate in ascending or descending order....? Any tips for the same
You can, simply search sorting dictionary keys :) That should do the job, lemme check if I can find already answered question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.