0

There are tow realm object class one is Entry and other is Work. The model scenario is looks like below.the Entry class is

open class Entry: Object {
  dynamic open var date = Entry.defaultDate()
  dynamic open var quantity = 0.0
  dynamic open var percentage = 0.0
  dynamic open var goal = 0.0
  open let gulps = List<Work>()
}

And the Work class looks like this.

open class Work: Object {
  dynamic var quantity = 0.0
  dynamic var type = 0
}

after inserting some data one Entry object looks like this.

(Entry {
    date = 2017-11-29;
    quantity = 1.193675890564919;
    percentage = 59.68379452824593;
    goal = 2;
    gulps = RLMArray <0x6040000e7800> (
        [0] Work {
            quantity = 0.5;
            type = 0;
        },
        [1] Work {
            quantity = 0.5;
            type = 1;
        },
        [2] Work {
            quantity = 0.09683794528245926;
            type = 0;
        },
        [3] Work {
            quantity = 0.09683794528245926;
            type = 2;
        }
    );
})

Now I am trying to make an array which contain the total quantity according to Work types where the array index is according to Work type.So the array for this entry should be.

[0.59683,0.5,0.09683794528245926]

So How can I generate this array in efficient way?

MD. Rejaul Hasan
  • 168
  • 1
  • 15

1 Answers1

0

This should be fairly simple, define an array of types that match the quantity property and then loop over the gulps array returning the quantity

let workArray: [Double] = entry.gulps.map({ return $0.quantity })

The map function will iterate through each item in the list and run the closure you define between the braces, in this case it will iterate through each work item and return only the quantity.

You can find out more about the map function here or the official Apple documentation is here

Here is a full working example I written in the playground

struct Entry {

    var quantity = 0.0
    var percentage = 0.0
    var goal = 0.0
    var gulps = [Work]()
}

struct Work {
    var quantity = 0.0
    var type = 0
}

let work1 = Work(quantity: 1.0, type: 5)
let work2 = Work(quantity: 2.0, type: 5)
let work3 = Work(quantity: 4.0, type: 5)
let work4 = Work(quantity: 6.0, type: 5)

let entry = Entry(quantity: 5.0, percentage: 50.0, goal: 60.0, gulps: [
    work1,
    work2,
    work3,
    work4
])

let workArray: [Double] = entry.gulps.map({ return $0.quantity })
print(workArray)

Output: [1.0, 2.0, 4.0, 6.0]

Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • thanks for your answer but this is not what I am asking for.The objects are not swift object it's a realm object and I also asking for sum them up according to there **work type** and make new array.So assume If you have 5 work object where the work type 1 is for 2 work object and work type 5 is for another 3 work object then my array should have 2 elements which are sum of type 1 and type 5. thanks. – MD. Rejaul Hasan Nov 29 '17 at 09:29
  • you asked for output like: `[0.59683,0.5,0.09683794528245926]`. if you want to group by total then you can use filter to get a list of all unique work types in the array and then use map and filter to get the totals for each work type – Scriptable Nov 29 '17 at 09:31
  • Actually I am not getting you.Can you edit your previous code so that I can understand your point.Another point I should say that the 'work type' is not static it's an input from user.I also looking for efficient coding because the actual database is not that small and the number of 'work' per 'entry' is quite big.It's good if I get any realm quarry .Thanks. – MD. Rejaul Hasan Nov 29 '17 at 10:11