arraysswiftmatrixcustom-data-type

How to use two-dimensions array in Swift?


So I have this array var arrayItinerario: Array<Any> = [] which is intended to hold arrays of type Itinerario, var temporalArray: Array<Itinerario> = []

Next, this arrayItinerario would later be used to access properties from the struct Itinerario from where the array temporalArray type comes from.

cell.siteLabel.text = "\([arrayItinerario[indexPath.section]][indexPath.row].ubicacion)"

The problem is that arrayItinerario is not an Itinerario type object which make it impossible to access ubicacion to make an example I have tried let object = [arrayItinerario[indexPath.section]][indexPath.row] as! Itinerario But the cast throws an error. How can I do to access properties from the arrays temporalArraythat are inside arrayItinerario?

Note: I am using indexPath because I am filling table cells



Solution

  • var arrayItinerario: [[Itinerario]] = []
    
    //fill data
    var temporalArray: [Itinerario] = []
    arrayItinerario.append(temporalArray)
    
    cell.siteLabel.text = "\(arrayItinerario[indexPath.section][indexPath.row].ubicacion)"
    

    or

    var arrayItinerario: Array<Any> = []
    var temporalArray: Array<Itinerario> = []
    
    guard let temp = arrayItinerario[indexPath.section] as? Array<Itinerario>{
        return
    }
    
    cell.siteLabel.text = "\(temp[indexPath.row].ubicacion)"