0

First, I have a struct like this:


struct CodigosAutorizacion {
    var codigo: String?
    var codigoCancel: String?
    var codigoSitio: String?
    var codigoCancelsitio: String?
    var instancia: String?
    init(
        code : String? = nil,
        codeCancel: String? = nil,
        codeSite: String? = nil,
        codeSiteCancel: String? = nil,
        instance: String? = nil
    ){
        self.codigo = code
        self.codigoCancel = codeCancel
        self.codigoSitio = codeSite
        self.codigoCancelsitio = codeSiteCancel
        self.instancia = instance
    }
}

This structure values are filled from a web service, then is stored in a array like this:

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)

now, when I try to give those values to a label inside a table cell(I use a custom cell), I cant access the specific structure value to give to the label. Example of what I am saying

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
    let codeCell: DetalleCodigoCell = tableView.dequeueReusableCell(withIdentifier: "CodigoCell", for: indexPath) as! DetalleCodigoCell
    codeCell.CodigoNombre.adjustsFontSizeToFitWidth = true
       
    codeCell.codigoNombre.text = codes![indexPath.row].instancia
    codeCell.codigoSitio.text = codes![indexPath.row].codigoSitio
    codeCell.codigoCancelSitio.text = codes![indexPath.row].codigoCancelsitio
    codeCell.codigoInstancia.text = codes![indexPath.row].codigo
    codeCell.codigoCancelInstancia.text = codes![indexPath.row].codigoCancel
      
    return codeCell
        
    }

I get the following errors in lines 'codes![indexPath.row].instancia' for example

errors:

-No exact matches in call to subscript

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

I have try accessing directly from 'codeArray' instead of storing it in the intermediate global array 'codes' and accessing but it doesn't work either, as I know this is suppose to be posible but I don't know why I am getting that error. Any solution? Thanks in advance.

This is a reference for what I am trying to do: How to pass specific value to a cell when that cell is selected?

  • According to the first snippet `codes` is `[[CodigosAutorizacion]]` which is a two-dimensioal array. However in `cellForRow` you treat it as `[CodigosAutorizacion]`, an one-dimensional array, which cannot work. – vadian Dec 29 '21 at 17:46
  • @vadian So pass the struct directly to the array, to keep it as a one-dimension one. Instead of appending codeArray which has the struct stored to array 'codes', just save the struct inside 'codes' and then in `cellForRow` access ´codes´. Is that what you mean? – Angel Davila Dec 29 '21 at 18:05
  • As an aside, try not to use `!` or `?` on optionals. It's better to unwrap your variable with a test for validity and do something useful on a failure. Remember that `!` means "crash if this is nil" and `?` means "silently do nothing if this is nil". So, unless those are what you *really* want, testing is better. – Phillip Mills Dec 29 '21 at 18:09
  • @PhillipMills Thanks I'll keep that in mind – Angel Davila Dec 29 '21 at 18:10
  • The code in `cellForRow` works if `codes` is `[CodigosAutorizacion]`. And why is it optional? Arrays are supposed to be declared non-optional empty. – vadian Dec 29 '21 at 18:30
  • @vadian `codes` is now `[CodigosAutorizacion]`, but still won´t let me access `.instancia` CodigosAutorizacion value at the `[indexPath.row].instancia` label assignation, the same error still appears. Also I fixed the optional array thing, thanks – Angel Davila Dec 29 '21 at 18:59

1 Answers1

0

Happy to help. it seems that the issues is about Optionals .

First: adjust your codes to type [CodigosAutorizacion]

Then: in cellForRowAt do some like var x = optional ?? nonOptional

example:

codeCell.codigoNombre.text = codes?[indexPath.row].instancia ?? "0"

reference: https://stackoverflow.com/a/25195633/17771995

Elevo
  • 359
  • 2
  • 10