我正在使用 Core Data 并且我的谓词之一根据以下枚举检索数据:
enum Period : Int {
Daily
Weekly
Monthly
}
Run Code Online (Sandbox Code Playgroud)
我的谓词是这样的:
public static func byTypePredicate(periods: [Int]) -> NSPredicate {
return NSPredicate(format: "period IN %@", periods)
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我不想Int's在调用这个谓词时使用,我想传递Period枚举,但在谓词内部必须将其转换Int为使其工作。
有没有快速转换的方法?
您可以使用map()(Sequence协议的)方法将每个枚举映射到其整数原始值:
func byTypePredicate(periods: [Period]) -> NSPredicate {
let intPeriods = periods.map { $0.rawValue } // [Int]
return NSPredicate(format: "period IN %@", intPeriods)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
671 次 |
| 最近记录: |