skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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 —

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search