iOS Swift 3 Check each date is valid, before filtering array

时间:2020-12-05 21:25:06
let dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd HH:mm:ss"

filtereditems = items.filter { item in return dateFormat.date(from: item.date)! >= Date()}

I have code above to filter items in my array, which are greater than or equal to todays date, which works fine.

我上面的代码来过滤我的数组中的项目,这些项目大于或等于今天的日期,这很好。

However, I want to check the dateFormat.date(from: item.date)! is valid as if the date can not parse or is not in the correct format, my app crashes out.

但是,我想检查dateFormat.date(来自:item.date)!有效,就好像日期无法解析或格式不正确,我的应用程序崩溃了。

I know about optionals, as below, but I'm not sure they fit together. e.g..

我知道选项,如下所示,但我不确定它们是否合在一起。例如..

if let temp_date = dateFormat.date(from: item.date) as? Date {
     //valid
} else {
     //invalid
}

Any help would be greatly appreciated? Thanks.

任何帮助将不胜感激?谢谢。

1 个解决方案

#1


3  

You are on the right track, but you need only the conditional binding, not the conditional cast with as? Data:

你是在正确的轨道上,但你只需要条件绑定,而不是条件转换为as?数据:

let filtereditems = items.filter { item in
    if let temp_date = dateFormat.date(from: item.date) {
        // valid date string: compare with current date
        return temp_date >= Date()
    } else {
        // invalid date string: skip
        return false
    }
}

Or – if you prefer – with guard:

或者 - 如果你愿意 - 带警卫:

let filtereditems = items.filter { item in
    guard let temp_date = dateFormat.date(from: item.date) else {
        // invalid date string: skip
        return false
    }
    // valid date string: compare with current date
    return temp_date >= Date()
}

Alternatively, use the nil-coalescing operator ?? to map invalid strings to a date in the past:

或者,使用零合并算子??将无效字符串映射到过去的日期:

let filtereditems = items.filter { item in
    return dateFormat.date(from: item.date) ?? Date.distantPast >= Date()
}

or with parameter shortcuts:

或使用参数快捷方式:

let filtereditems = items.filter {
    dateFormat.date(from: $0.date) ?? Date.distantPast >= Date()
}

Remark: A better approach might be to change the type of date in your item class from String to Date?, and do the conversion during initialization. That might save repeated string to date conversions.

备注:更好的方法可能是将项类中的日期类型从String更改为Date?,并在初始化期间进行转换。这可能会保存重复的字符串到日期转换。

#1


3  

You are on the right track, but you need only the conditional binding, not the conditional cast with as? Data:

你是在正确的轨道上,但你只需要条件绑定,而不是条件转换为as?数据:

let filtereditems = items.filter { item in
    if let temp_date = dateFormat.date(from: item.date) {
        // valid date string: compare with current date
        return temp_date >= Date()
    } else {
        // invalid date string: skip
        return false
    }
}

Or – if you prefer – with guard:

或者 - 如果你愿意 - 带警卫:

let filtereditems = items.filter { item in
    guard let temp_date = dateFormat.date(from: item.date) else {
        // invalid date string: skip
        return false
    }
    // valid date string: compare with current date
    return temp_date >= Date()
}

Alternatively, use the nil-coalescing operator ?? to map invalid strings to a date in the past:

或者,使用零合并算子??将无效字符串映射到过去的日期:

let filtereditems = items.filter { item in
    return dateFormat.date(from: item.date) ?? Date.distantPast >= Date()
}

or with parameter shortcuts:

或使用参数快捷方式:

let filtereditems = items.filter {
    dateFormat.date(from: $0.date) ?? Date.distantPast >= Date()
}

Remark: A better approach might be to change the type of date in your item class from String to Date?, and do the conversion during initialization. That might save repeated string to date conversions.

备注:更好的方法可能是将项类中的日期类型从String更改为Date?,并在初始化期间进行转换。这可能会保存重复的字符串到日期转换。