I'm just beginning to understand the use of flatMap, etc. To make filters to my arrays. The problem is that I don't understand well how to make filters within others to make my search more efficient, specifically I need to extract the elements of "nombre" in the array "RA" (RA is contained in the array " Actividades", therefore, there may be several "RA" arrays). The filter in this case is that Planificacion -> idCurso == 61, Semana -> semana == "S7", Clase -> clase == "C1". There is also the possibility that there are no "RA" elements with the filter.
class Planificacion: NSObject, NSCoding {
let idCurso: Int
let semana: [Semana]
init(idCurso: Int, semana: [Semana]) {
self.idCurso = idCurso
self.semana = semana
}
}
class Semana: NSObject, NSCoding {
let semana: String
let id: Int
let numero: Int
let clase: [Clase]
init(semana: String, id: Int, numero: Int, clase: [Clase]) {
self.semana = semana
self.id = id
self.numero = numero
self.clase = clase
}
}
class Clase: NSObject, NSCoding {
let clase: String
let id: Int
let numero: Int
let fecha_progr: String
let actividades: [Actividades]
init(clase: String, id: Int, numero: Int, fecha_progr: String, actividades: [Actividades]) {
self.clase = clase
self.id = id
self.numero = numero
self.fecha_progr = fecha_progr
self.actividades = actividades
}
}
class Actividades: NSObject, NSCoding {
let id: Int
let texto: String
let tipo: String
let ra: [RA]
init(id: Int, texto: String, tipo: String, ra: [RA]) {
self.id = id
self.texto = texto
self.tipo = tipo
self.ra = ra
}
}
class RA: NSObject, NSCoding {
let id: Int
let nombre: String
init(id: Int, nombre: String) {
self.id = id
self.nombre = nombre
}
}
var arrayPlanificacion = [Semana]()
if let sale = PlanificacionAll.first(where: { $0.idCurso == 61 }) {
arrayPlanificacion = sale.semana
arrayPlanificacion.sort { $0.numero < $1.numero }
}
if let clase = arrayPlanificacion.first(where: { $0.semana == "S7" }) {
for i in clase.clase {
if let inClase = clase.clase.first(where: { $0.clase == "C1" }){
var actividades = [Actividades]()
actividades = inClase.actividades
...
}
}
}