0

Swift 3.0 xcode 9.4.1 から Swift 4.2 xcode 10.2.1 へのコード移行の一環として、Swift 3.0 で使用される配列ソート方法の問題に直面しています。 4.2 機能しません。並べ替えられた配列リストの代わりに nil 値を返します。

// Statuses currently come through in an array, which is not sorted in the correct order
// The order is specific, as specified in the enum list
// Create a pre-made list of section headers, only for statuses that have valid activities

Leave は JSONEncodable モーダル クラスで、Status は内部で宣言された列挙型文字列です。どんな助けでも感謝します。前もって感謝します。

open class Leave: JSONEncodable {

    public enum Status: String { 
        case Drafts = "Drafts"
        case PushedBack = "Pushed Back"
        case PendingApproval = "Pending Approval"
        case UpcomingLeave = "Upcoming Leave"
        case History = "History"
        case Booked = "Booked"
        case Approved = "Approved"
        case Denied = "Denied"
        case ApprovedAndDeniedRequest = "Approved and Denied Request"
        case Error = "Error"
    }
}

var orderedSections: [Leave.Status] {
    var list: [Leave.Status] = []
    for status in iterateEnum(Leave.Status.self) {
        list.append(status)
    }
    return list
}


fileprivate func iterateEnum<T: Hashable>(_: T.Type) -> AnyIterator<T> {
    var i = 0
    return AnyIterator {
        let next = withUnsafePointer(to: &i) { $0.withMemoryRebound(to: T.self, capacity: 1) { $0.pointee } }
        let res: T? = next.hashValue == i ? next : nil
        i += 1
        return res
    }
}
4

2 に答える 2