简体   繁体   English

如何在 Swift 中对具有关联值的枚举数组进行排序?

[英]How can I sort an array of enums with associated values in Swift?

First question on StackOverflow… Please be kind.关于 StackOverflow 的第一个问题……请善待。 :) :)

I have an array of enums with associated values which I would like to sort chronological.我有一个带有关联值的枚举数组,我想按时间顺序对其进行排序。 One of these values contains a date (as a String) which gets displayed and should be used for sorting.其中一个值包含一个日期(作为字符串),该日期被显示并应用于排序。

Here is what it looks like at the moment:这是目前的样子:

enum cellType {
    case standard(text: String, icon: UIImage, hasChildren: Bool, reference: String)
    case detailed(text: String, secondaryText: String, icon: UIImage, hasChildren: Bool, reference: String)

    var identifier: String {
        switch self {
        case .standard: return "standardCell"
        case .detailed: return "detailedCell"
        }
    }
}

var cellData: [cellType]

Then the cellData gets populated with some cellType s.然后cellData被填充一些cellType s。 The value for secondaryText contains the date and it looks like this: secondaryText的值包含日期,如下所示:

print(cellData) 

// [
//    AppIdentifier.cellType.detailed(text: "Title A", secondaryText: "2017-10-20T10:04:00.000+02:00", icon: <UIImage: 0x60c0002a6660>, {12, 21}, hasChildren: false, reference: "40693"),
//    AppIdentifier.cellType.detailed(text: "Title B", secondaryText: "2016-12-14T10:04:00.000+02:00", icon: <UIImage: 0x60c0002a6660>, {12, 21}, hasChildren: false, reference: "40632")
//    AppIdentifier.cellType.detailed(text: "Title C", secondaryText: "2017-10-20T10:07:00.000+02:00", icon: <UIImage: 0x60c0002a6660>, {12, 21}, hasChildren: false, reference: "40694"),
//    AppIdentifier.cellType.detailed(text: "Title D", secondaryText: "2017-11-16T10:34:00.000+02:00", icon: <UIImage: 0x60c0002a6660>, {12, 21}, hasChildren: false, reference: "40633"),
//    AppIdentifier.cellType.detailed(text: "Title E", secondaryText: "2017-10-19T10:12:00.000+02:00", icon: <UIImage: 0x60c0002a6660>, {12, 21}, hasChildren: false, reference: "40682"),
// ]

When I tried to sort the array, the following was apparently too simple:当我尝试对数组进行排序时,以下内容显然太简单了:

var cellDataSorted = cellData.sorted(by: {$0.detailed.secondaryText < $1.detailed.secondaryText})

// Swift Compiler Error: enum element ’detailed’ cannot be referenced as an instance member

What would be the best way to sort this array?对这个数组进行排序的最佳方法是什么? Thanks in advance!提前致谢!

(I am writing this on the phone, so.. fix the syntax if necessary) (我是在电话上写这个的,所以.. 如有必要,请修复语法)

Use following code within the sorted function:在 sorted 函数中使用以下代码:

if case .detailed(_, let date0, _, _, _) = $0, case .detailed(_, let date1, _, _, _) = $1 {
     return date0 < date1
} else {
    // One of them didn't have date (should never happen)
    return true
}

Your problem is, that you are accessing the parameters of the enum as if it was an instance of a class/struct.您的问题是,您正在访问枚举的参数,就好像它是类/结构的实例一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM