I'm trying to take an array or JSON data and group the values in the array by date using the following code:
func groupTrips (trips: JSON) -> [[Trip?]] {
let calendar = NSCalendar.currentCalendar()
var groupedTrips:[[Trip?]] = []
var group: [Trip] = []
for (index, tripRaw):(String, JSON) in trips {
let trip = Trip(trip: tripRaw)
if index == "0" && calendar.compareDate(trip.pickup, toDate: NSDate.init(), toUnitGranularity: .Day) != .OrderedSame {
groupedTrips.append([nil])
}
if let lastTrip = group.last {
let order = calendar.compareDate(trip.pickup, toDate: lastTrip.pickup, toUnitGranularity: .Day)
if order == .OrderedSame {
group.append(trip)
} else {
groupedTrips.append(group)
group = [trip]
}
} else {
group.append(trip)
}
}
groupedTrips.append(group)
return groupedTrips
}
When I try to run the code, I get an error on the 3rd to last line groupedTrips.append(group): Thead 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) And in my console, I get fatal error: array cannot be bridged from Objective-C. My Trip class is just a simple Swift class that parsed the JSON element into objects.