从字典中获取日期值

时间:2019-03-07 21:25:29

标签: ios swift realm

我有一本这样声明的字典:

var result:[Workout] = []

结果值如下,使用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;
}]

我只想获取每次锻炼的日期。我尝试了一些不同的方法,而最接近的方法是使用此方法:

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)
    }
}

问题在于它只打印出第一个值13次:

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)

有什么想法/提示吗?

2 个答案:

答案 0 :(得分:0)

您可以尝试

let res = result.filter { compareDate(date1:$0.date, date2:savedDate) }
print(res)

答案 1 :(得分:0)

我认为您是在问如何迭代对象数组并返回date属性?

我不知道你的健身对象是什么样子,所以我做了一个

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)

输出为

["2019-03-06", "2019-02-24", "2019-02-22"]