Kobe Kobe - 1 year ago 164
iOS Question

How to convert array of enum to array of int's

I am using Core Data and one of my predicates retrieves data based on the following enum:

enum Period : Int {

My predicates is like this:

public static func byTypePredicate(periods: [Int]) -> NSPredicate {
return NSPredicate(format: "period IN %@", periods)

My problem is I don't want to use
when calling this predicate, I want to pass the
enum, but inside the predicate is have to convert it to
to make it work.

Is there a quick way to convert it ?

Answer Source

You can use the map() method (of the Sequence protocol) to map each enumeration to its integer raw value:

func byTypePredicate(periods: [Period]) -> NSPredicate {
    let intPeriods = periods.map { $0.rawValue } // [Int]
    return NSPredicate(format: "period IN %@", intPeriods)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download