0

I have a dictionary declared like this:

var result:[Workout] = []

The value in result is following, using print(result):

[Workout {
    date = 2019-03-06 22:18:41 +0000;
    name = Legday;
    exercise = Bbb;
    sets = 2;
    reps = 1;
    kg = 77;
    notes = Bb;
}, Workout {
    date = 2019-02-24 18:41:07 +0000;
    name = bvcj;
    exercise = Barbell Bench Press;
    sets = 1;
    reps = 1;
    kg = 87;
    notes = Kjj;
}, Workout {
    date = 2019-02-22 08:02:23 +0000;
    name = Chest;
    exercise = Barbell Bench Press;
    sets = 3;
    reps = 6;
    kg = 95;
    notes = Ok med spot;
}, Workout {
    date = 2019-02-22 08:02:23 +0000;
    name = Chest;
    exercise = Dips;
    sets = 2;
    reps = 6;
    kg = 40;
    notes = Lett, 1 ektra rep på siste sett;
}, Workout {
    date = 2019-02-22 08:02:23 +0000;
    name = Chest;
    exercise = Incline Barbell Bench Press;
    sets = 2;
    reps = 6;
    kg = 72.5;
    notes = Tungt;
}, Workout {
    date = 2019-02-22 08:02:23 +0000;
    name = Chest;
    exercise = Triceps Pushdown;
    sets = 3;
    reps = 12;
    kg = 14.5;
    notes = Ok;
}, Workout {
    date = 2019-02-19 13:38:35 +0000;
    name = Legday;
    exercise = Squat;
    sets = 2;
    reps = 2;
    kg = 77.5;
    notes = Lett;
}, Workout {
    date = 2019-02-19 13:38:35 +0000;
    name = Legday;
    exercise = Leg Press;
    sets = 9;
    reps = 8;
    kg = 100;
    notes = Tungt;
}, Workout {
    date = 2019-02-18 10:54:34 +0000;
    name = Legday;
    exercise = Barbell Squat;
    sets = 3;
    reps = 6;
    kg = 75;
    notes = Tungt;
}, Workout {
    date = 2019-02-18 10:54:34 +0000;
    name = Legday;
    exercise = Leg Extension;
    sets = 10;
    reps = 10;
    kg = 32;
    notes = Lett;
}, Workout {
    date = 2019-02-17 21:48:52 +0000;
    name = Legday;
    exercise = Squat;
    sets = 3;
    reps = 6;
    kg = 60;
    notes = Lett;
}, Workout {
    date = 2019-02-17 21:48:52 +0000;
    name = Legday;
    exercise = Tåhev;
    sets = 4;
    reps = 12;
    kg = 40;
    notes = Tungt;
}, Workout {
    date = 2019-02-17 21:48:52 +0000;
    name = Legday;
    exercise = Leg Extension;
    sets = 3;
    reps = 6;
    kg = 43.5;
    notes = Lett;
}]

I want to get only the date, for each workout. I have tried some different things, and the closest I've got, was using this:

var remoteIndexPath = NSIndexPath(row: 0, section: 0)
func didSelectDayView(_ dayView: DayView, animationDidFinish: Bool) {

    let row = result[remoteIndexPath.row]

    for i in result.indices { // Prøv dette i stedet for: for i in 0 ..< Results.count {
        print(result[remoteIndexPath.row].date)
    }
}

The problem is that it only prints out the first value 13 times:

Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)
Optional(2019-03-06 22:18:41 +0000)

Any ideas/tips?

Jay
  • 29,230
  • 15
  • 46
  • 73
Trinity
  • 15
  • 4
  • That's an array, not a dictionary. I added an additional answer that should be fairly straightforward. – Jay Mar 07 '19 at 22:16

2 Answers2

0

You can try

let res = result.filter { compareDate(date1:$0.date, date2:savedDate) }
print(res)
Sh_Khan
  • 86,695
  • 6
  • 38
  • 57
  • The first example only get the first date, the same as I have in the end of my question. I want to go through the whole dictionary, and get the dates that is equal to another date I have saved. So I only want to pris out those dates that is equal. – Trinity Mar 07 '19 at 21:35
  • read your saved date and use the above way , btw `result` is an array not dictionary – Sh_Khan Mar 07 '19 at 21:38
  • Ah, ok, my bad.. Looks like it won't show the data at all, since the stored data is stored like: `2019-02-22 08:02:23 +0000 `, and I am looking for `2019-02-22 00:00:00 +0000 `. Looks like it is because of the hours/seconds. How can I bypass that? – Trinity Mar 07 '19 at 21:48
  • use this method https://stackoverflow.com/a/47275545/5820010 for the day comparison – Sh_Khan Mar 07 '19 at 21:52
  • Looks like that only return boolean value, not the actual dates I want to get? – Trinity Mar 07 '19 at 22:00
  • yes , but you will use it inside the filter see edit – Sh_Khan Mar 07 '19 at 22:01
  • Ah, I didn't put it inside the filter. Thank you so much :) You saved the day!! – Trinity Mar 07 '19 at 22:11
0

I think you are asking how to iterate over an array of objects and return the date property?

I don't know what your workout object looks like so I made one up

class Workout {
    var date = ""
    var name = ""

    init(aDate: String, aName: String) {
        self.date = aDate
        self.name = aName
    }
}

var results:[Workout] = []

let w0 = Workout(aDate: "2019-03-06", aName: "Legday")
let w1 = Workout(aDate: "2019-02-24", aName: "bvjc")
let w2 = Workout(aDate: "2019-02-22", aName: "Chest")

results.append(w0)
results.append(w1)
results.append(w2)

let allDates = results.map{ $0.date } //creates an array of just the dates
print(allDates)

and the output is

["2019-03-06", "2019-02-24", "2019-02-22"]
Jay
  • 29,230
  • 15
  • 46
  • 73