Swift 2 - 如果值早于当前日期,则删除数组中的 NSDate 值
Swift 2 - remove NSDate values in array if value is before current date
在我的应用程序中,我创建了一个 "NSDate" 数组以发送本地通知。
保存的值是 "UUID" 和 "deadline",它们是使用 let gameDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(GAME_INFO) ?? [:]
保存的
结果与此类似:
[{
UUID = "546C5E4D-CFEE-42F3-9010-9936753D17D85";
deadline = "2015-12-25 15:44:26 +0000";
}, {
UUID = "7C030614-C93C-4EB9-AD0A-93096848FDC7A";
deadline = "2015-12-25 15:43:15 +0000";
}]
我想要实现的是将 "deadline" 值与当前日期进行比较,如果截止日期早于当前日期,则需要从数组中删除这些值。
func compareDeadline() {
let gameDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(GAME_INFO) ?? [:]
var items = Array(gameDictionary.values)
for i in 0..<items.count {
let dateNotification = items[i]["deadline"]!! as! NSDate
print(dateNotification)
var isOverdue: Bool {
return (NSDate().compare(dateNotification) == NSComparisonResult.OrderedDescending) // deadline is earlier than current date
}
print(isOverdue)
if (isOverdue == true){
items.removeAtIndex(i)
}
}
}
当我尝试从数组中删除值时,我得到 Fatal Error: Array index out of range
知道我该如何解决这个问题吗?
您应该对数组使用 .filter 方法来删除该数组中不需要的任何内容。结果是一个只有过滤结果的新数组。
.filter 要求您在发送给它的闭包中设置过滤条件
您可以使用 swift 数组的 filter
方法
例如过滤数组中的偶数:
func isEven(number: Int) -> Bool {
return number % 2 == 0
}
evens = Array(1...10).filter(isEven)
println(evens)
有一些问题。您收到错误的原因是因为您 cannot 在 for-in 块内迭代时删除元素。您可以使用以下代码过滤项目:
func compareDeadline() {
let gameDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(GAME_INFO) ?? [:]
let items = Array(gameDictionary.values)
let currentDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
let filteredItems = items.flatMap({
guard let stringDeadline = [=10=]["deadline"] as? String, let deadline = dateFormatter.dateFromString(stringDeadline) else {
return nil
}
return deadline
}).filter({
currentDate.compare([=10=]) == .OrderedDescending
})
}
在我的应用程序中,我创建了一个 "NSDate" 数组以发送本地通知。
保存的值是 "UUID" 和 "deadline",它们是使用 let gameDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(GAME_INFO) ?? [:]
结果与此类似:
[{
UUID = "546C5E4D-CFEE-42F3-9010-9936753D17D85";
deadline = "2015-12-25 15:44:26 +0000";
}, {
UUID = "7C030614-C93C-4EB9-AD0A-93096848FDC7A";
deadline = "2015-12-25 15:43:15 +0000";
}]
我想要实现的是将 "deadline" 值与当前日期进行比较,如果截止日期早于当前日期,则需要从数组中删除这些值。
func compareDeadline() {
let gameDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(GAME_INFO) ?? [:]
var items = Array(gameDictionary.values)
for i in 0..<items.count {
let dateNotification = items[i]["deadline"]!! as! NSDate
print(dateNotification)
var isOverdue: Bool {
return (NSDate().compare(dateNotification) == NSComparisonResult.OrderedDescending) // deadline is earlier than current date
}
print(isOverdue)
if (isOverdue == true){
items.removeAtIndex(i)
}
}
}
当我尝试从数组中删除值时,我得到 Fatal Error: Array index out of range
知道我该如何解决这个问题吗?
您应该对数组使用 .filter 方法来删除该数组中不需要的任何内容。结果是一个只有过滤结果的新数组。
.filter 要求您在发送给它的闭包中设置过滤条件
您可以使用 swift 数组的 filter
方法
例如过滤数组中的偶数:
func isEven(number: Int) -> Bool {
return number % 2 == 0
}
evens = Array(1...10).filter(isEven)
println(evens)
有一些问题。您收到错误的原因是因为您 cannot 在 for-in 块内迭代时删除元素。您可以使用以下代码过滤项目:
func compareDeadline() {
let gameDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(GAME_INFO) ?? [:]
let items = Array(gameDictionary.values)
let currentDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ssZ"
let filteredItems = items.flatMap({
guard let stringDeadline = [=10=]["deadline"] as? String, let deadline = dateFormatter.dateFromString(stringDeadline) else {
return nil
}
return deadline
}).filter({
currentDate.compare([=10=]) == .OrderedDescending
})
}