portal47 portal47 - 10 months ago 62
Swift Question

Make a reverse filter

I have a tableview and I want to filter the data to show only the cells witch has a different date than today, like a reverse search.

I have this to show the cells with a certain date:

filtered = events.filter({ (aEvent : Tareas) -> Bool in
let tmp: NSString = aEvent.eventStatus
let range = tmp.rangeOfString("2016-11-24", options: NSStringCompareOptions.LiteralSearch)
return range.location != NSNotFound

I need to do the oposite, show the cells with a different date, is that posible?

Answer Source

You're massively overcomplicating this. There's no reason at all to be using Foundation data types here.

To keep events that do contain the desired date:

let desiredDate = "2016-11-24"
filtered = events.filter { $0.eventStatus.range(of: desiredDate) != nil }

To keep events that don't contain the desired date:

filtered = events.filter { $0.eventStatus.range(of: desiredDate) == nil }