am尝试对字符串进行子字符串化,然后在两个字符串之间进行比较,依此类推。
我尝试过:
for matchdate in matches.data! {
datesarray.append(matchdate.dateGregorian!)
}
datesarray = datesarray.reduce([], {
$0.contains($1) ? $0 : $0 + [$1]
})
datesarray = datesarray.sorted()
for date in datesarray {
matchesarray.removeAll()
for match in matches.data! {
if date.prefix(10) == match.dateGregorian?.prefix(10) {
.....
}
}
}
这很好..但是datearray不是唯一的,我需要像这样子字符串化:
for matchdate in matches.data! {
datesarray.append(String(matchdate.dateGregorian!.prefix(10)))
}
datesarray = datesarray.reduce([], {
$0.contains($1) ? $0 : $0 + [$1]
})
datesarray = datesarray.sorted()
for date in datesarray {
matchesarray.removeAll()
for match in matches.data! {
if date == String(match.dateGregorian!.prefix(10)) {
.....
}
}
}
但这不会做任何事情!我没有错误,但是结果没有显示!
为什么?以及如何解决这个问题?
答案 0 :(得分:0)
如@vadian所说..我只是将它们转换为日期:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "en")
let date = dateFormatter.date(from:matchdate.dateGregorian!)!
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)
let finalDate = calendar.date(from:components)
然后像这样在它们之间进行比较:
if matchdate == finalDate{
.....
}
问题解决了,现在一切正常:)!