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?
You can try
let res = result.filter { compareDate(date1:$0.date, date2:savedDate) }
print(res)
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"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.