Dan*_*rca -1 arrays sorting ios swift
为了对具有布尔值,整数和日期的自定义结构的数组进行排序。我已成功将以下语法用于布尔值,并且适用于“新娘”和“新郎”的情况。当我尝试为2个Date变量添加排序时,出现以下错误错误:
“二进制运算符'>'不能应用于两个'Date?' 操作数”
我的印象是,可以用类似的方式将Date值与正常> < ==条件进行比较,但是我想我会收到错误,因为这些值未展开?如果是正确的话,我认为我不能做一个“让日期”吗?转换为未包装的日期,因此我不确定如何比较这些值。
var sortedImages = [submitted_image]()
switch sortOption {
case .brideInPic:
print("bride")
sortedImages = Images.sorted(by: {$0.brideInPic && !$1.brideInPic})
print("sortedImages: \(sortedImages.count), Images: \(Images.count)")
case .groomInPic:
print("groom")
sortedImages = Images.sorted(by: {$0.groomInPic && !$1.groomInPic})
print("sortedImages: \(sortedImages.count), Images: \(Images.count)")
case .create_dt:
print("create")
sortedImages = Images.sorted(by: {$0.create_dt > $1.create_dt})
}
Run Code Online (Sandbox Code Playgroud)
不能直接比较可选项(比较SE-0121 –删除可选比较运算符)。但是您可以使用nil-coalescing运算符??为没有创建日期的条目提供默认日期:
Images.sorted(by: {$0.create_dt ?? .distantPast > $1.create_dt ?? .distantPast })
Run Code Online (Sandbox Code Playgroud)
对于.distantPast没有创建日期的条目,将其排序到列表的末尾。随着.distantFuture他们将被排序到列表的开头。