Diciamo che ho un'app che mostra i dettagli di una persona. Ogni persona può avere zero o più numeri di telefono e zero o più note allegate ad esso.
Pertanto, ho un'entità Core Data   Person    con relazioni uno-a-molti per l'entità   Phone    e l'entità   Note   .
Voglio mostrarli in   UITableView   , dove c'è una sezione "telefoni" e una sezione "note". 
 Quindi,   numberOfRowsInSection    sarà simile a questo: 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0: // Phones
        if self.person.phones.count > 0 {
            return self.person.phones.count
        } else {
            return 1 // We want a different cell to display "no phones"
        }
    case 1: // Notes
        if self.person.notes.count > 0 {
            return self.person.notes.count
        } else {
            return 1 // We want a different cell to display "no notes"
    default:
        return 0
    }
}
 E   cellForRowAt    sarà simile a questo: 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.section {
    case 0:
        if self.person.phones.count > 0 {
            return UITableViewCell.init(style: .default, reuseIdentifier: "PhoneNumberCell")
        } else {
            return UITableViewCell.init(style: .default, reuseIdentifier: "NoPhoneNumbersCell")
        }
    case 1:
        if self.person.notes.count > 0 {
            return UITableViewCell.init(style: .default, reuseIdentifier: "NoteCell")
        } else {
            return UITableViewCell.init(style: .default, reuseIdentifier: "NoNotesCell")
        }
    default:
        return UITableViewCell.init(style: .default, reuseIdentifier: "default")
    }
}
 E quindi, puoi già indovinare, lo stesso codice si ripeterà per   willDisplay   : 
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    switch indexPath.section {
    case 0:
        if self.person.phones.count > 0 {
            // Configure Phone Cell
        } else {
            // Configure No Phones Cell
        }
    case 1:
        if self.person.notes.count > 0 {
            // Configure Note Cell
        } else {
            // Configure No Notes Cell
        }
    default: break
    }
}
 Lo stesso vale per   didSelectRowAt    (e altri metodi delegato / origine dati) ma non ne copierò più qui. 
 Quello che voglio fare è eliminare questa lunga e ripetuta   switch    con l'istruzione   if..else    ma il problema è che in alcuni casi lo uso per restituire un numero, in altri una stringa e in altri per configurare semplicemente una cellula. 
Qualcuno ha un'idea o un modello da consigliare per questo caso?