Я пишу код, чтобы отфильтровать поездки от датчика движения. Я понял, что лучший способ сделать это, чтобы добавить подмассив в гнездовой массив на основе следующий:Фильтр-массив на основе следующего объекта
- Detect первого появление уверенного автомобильного события
- Добавить все следующие события движения к тому же массив событий до первого уверенного наблюдения, в котором говорится иначе.
Например
automotive confidence 2 //Add
automotive confidence 2 //Add
automotive confidence 2 //Add
walking confidence 2 //Add the sub-array to the master array and start over on the next confident automotive event.
В настоящее время я делаю это так:
//Remove all uncertain values.
let confidentActivities = activities!.filter{$0.confidence.rawValue == 2}
var needsNew = true
var automotiveActivities:Array<Array<CMMotionActivity>> = Array() //Master array to contain subarrays of automotiveactivity arrays
var automotiveActivitySession:Array<CMMotionActivity> = Array()
for activity in confidentActivities {
if activity.automotive && (!activity.cycling && !activity.running && !activity.walking){
if needsNew {
needsNew = false
}
automotiveActivitySession.append(activity)
} else {
if !needsNew {
//If user is no longer in car, store a cpoy of the session and reset the array
automotiveActivities.append(Array(automotiveActivitySession))
automotiveActivitySession = []
needsNew = true
}
}
}
Это решение не очень элегантно. Есть ли способ использовать Swift's Array.filter{}
, чтобы сделать сортировку более красивой?