0

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    
            ...  
        }
    }
}
  • In your code you are making unnecessary loop because you are still using the clase.clase.first don't get that one. – Nirav D Mar 02 '17 at 05:11
  • is it the reason why I ask the question, I feel that I am doing loops inefficient and unnecessary. I'm extracting the information of each array to enter and filter the following, but I think what is there a way to do it without doing this, or am I wrong? – Diego Salinas Flores Mar 02 '17 at 12:53
  • What I'm saying is you get actividades array with first closure so no need to use for loop now simply filter on actividades array. – Nirav D Mar 02 '17 at 13:45

0 Answers0