Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在Swift中对具有关联值的枚举数组进行排序?_Arrays_Swift_Sorting_Enums - Fatal编程技术网

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

Arrays 如何在Swift中对具有关联值的枚举数组进行排序?,arrays,swift,sorting,enums,Arrays,Swift,Sorting,Enums,关于StackOverflow的第一个问题……请客气点。:) 我有一个带有关联值的枚举数组,我想按时间顺序排序。其中一个值包含一个日期(作为字符串),该日期将显示并应用于排序 以下是目前的情况: enum cellType { case standard(text: String, icon: UIImage, hasChildren: Bool, reference: String) case detailed(text: String, secondaryText: Stri

关于StackOverflow的第一个问题……请客气点。:)

我有一个带有关联值的枚举数组,我想按时间顺序排序。其中一个值包含一个日期(作为字符串),该日期将显示并应用于排序

以下是目前的情况:

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]
然后用一些
cellType
s填充
cellData
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"),
// ]
打印(cellData)
// [
//AppIdentifier.cellType.detailed(文本:“标题A”,第二文本:“2017-10-20T10:04:00.000+02:00”,图标:,{12,21},hasChildren:false,参考:“40693”),
//AppIdentifier.cellType.detailed(文本:“标题B”,第二文本:“2016-12-14T10:04:00.000+02:00”,图标:,{12,21},hasChildren:false,参考:“40632”)
//AppIdentifier.cellType.detailed(文本:“标题C”,第二文本:“2017-10-20T10:07:00.000+02:00”,图标:,{12,21},hasChildren:false,参考:“40694”),
//AppIdentifier.cellType.detailed(文本:“标题D”,第二文本:“2017-11-16T10:34:00.000+02:00”,图标:,{12,21},hasChildren:false,参考:“40633”),
//AppIdentifier.cellType.detailed(文本:“标题E”,第二文本:“2017-10-19T10:12:00.000+02:00”,图标:,{12,21},hasChildren:false,参考:“40682”),
// ]
当我尝试对数组进行排序时,以下内容显然过于简单:

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

// Swift Compiler Error: enum element ’detailed’ cannot be referenced as an instance member
var cellDataSorted=cellData.sorted(按:{$0.detailed.secondaryText<$1.detailed.secondaryText})
//Swift编译器错误:枚举元素“detailed”不能作为实例成员引用
排序此数组的最佳方法是什么?提前谢谢

(我是在电话上写的,所以..如果需要,请修改语法)

在排序函数中使用以下代码:

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
}
如果case.detailed(u,let date0,u,u,u,u)=$0,case.detailed(u,let date1,u,u,u)=$1{
返回日期0<日期1
}否则{
//其中一个没有日期(不应该发生)
返回真值
}

您的问题是,您正在访问枚举的参数,就像它是类/结构的实例一样。

我建议您在Swift中搜索如何对枚举进行排序和数组。我相信这个问题以前已经得到了回答。如果没有,肯定有一些问题可以帮助你自己回答。@sasquatch我读了那一页。请告诉我你不喜欢什么,这样我可以修复它,下次做得更好。谢谢。@Fogmeister我做了搜索,读了几篇关于数组中枚举排序的文章。我既没有为相关的价值找到具体的东西,也无法通过重构其他方法自己解决它。也许我对这个快速的东西太陌生了……但是,我不会问其他帖子什么时候给了我一个正确方向的提示。现在感觉很傻…:-/好吧,当我回到笔记本电脑时,我可以添加一个答案。但这与这里的其他答案非常相似。我想。无论如何,第一步是遵守“可比”协议。一旦这样做了,你的分类就微不足道了:-)谢谢@Daniel。它按预期工作,数组得到正确排序。但是,需要将
date0
date1
定义为常量:
let date0
let date1
。也许你可以相应地修改你的答案?