Home > Mobile >  How can I access an struct attribute from an array?
How can I access an struct attribute from an array?

Time:12-31

What I want to do is to, after storing several objects of type CodigosAutorizacion inside an array:

for value in 0..<(array.count) {
  let codeArray = CodigosAutorizacion(
      code: validateData!["codigo"] as? String,
      codeCancel: validateData!["cancela_codigo"] as? String,
      codeSite: validateData!["cod_sitio"] as? String,
      codeSiteCancel: validateData!["cancela_cod_sitio"] as? String,
      instance: validateData!["instancia"] as? String
    )
  codes.append(codeArray)

}

Access the object attributes from the array like this:

 codeCell.codigoSitio.text = codes[indexPath.row].instance

This piece throws me the next errors

-No exact matches in call to subscript

-Reference to member 'instancia' cannot be resolved without a contextual type

this is because ´codes´ is an array and not a CodigosAutorizacion type

Storing these objects in an array is important because I will need to generate a table with several of this CodigosAutorizacion objects. Is there any way this can be possible?

CodePudding user response:

var codes = [CodigosAutorizacion]()

for value in 0..<(array.count) {
  let codeArray = CodigosAutorizacion(
    code: validateData!["codigo"] as? String,
          codeCancel: validateData!["cancela_codigo"] as? String,
          codeSite: validateData!["cod_sitio"] as? String,
          codeSiteCancel: validateData!["cancela_cod_sitio"] as? String,
          instance: validateData!["instancia"] as? String
    )
    codes.append(codeArray)
}

-- Try this --

CodePudding user response:

Fixed! it was a problem with the codes array declaration, it was not supposed to be var codes: Array<Any> but instead var codes: Array<CodigosAutorizacion> in order to access CodigosAutorizacion's properties

  • Related