1
votes

I got this error:

cannot convert value of type [Destinos] to type [String] in coercion swift

I have this Struct:

public struct Destinos: Data { public var idDestino : Int? public var desDestino : String? }

and this:

var listado = [Destinos]() listado.append(Destinos(idDestino: 1, desDestino: "Asunción")) listado.append(Destinos(idDestino: 2, desDestino: "Miami"))

then:

var ListaDeDestinos = [String]()

so my error appears in this line:

ListaDeDestinos = DestinoLista.listado as [String]

What is wrong here? can help me please? i´m don´t find anything like this in the forums

Edit all my code:

import UIKit

class Api {

let Presentador: DestinoPresenter! // referenciamos la clase DestinoPresenter en una variable local

init(Present: DestinoPresenter){ // constuctor: inicializamos la variable Presentador y creamos una copia de la clase DestinoPresenter
    Presentador = Present
}

var listado = [Destinos]()


func GetDestinos(){


    listado.append(Destinos(idDestino: 1, desDestino: "Asunción"))
    listado.append(Destinos(idDestino: 2, desDestino: "Miami"))


    print(listado)

}

}

class DestinoPresenter {

let mview: ViewController // referenciamos la clase DestinoPresenter en una variable local

init(view: ViewController) { //construnctor
    mview = view
}

var ArrayAutoComplete = [String]()
var ListaDeDestinos = [String]()
fileprivate var DestinoLista: Api!


func searchDestinos(_ substring: String) {

    ArrayAutoComplete.removeAll() //cada vez que llamemos a esta funcion, limpiamos la variable ArrayAutoComplete del TableView

    DestinoLista = Api(Present: self)
    DestinoLista.GetDestinos()

// ListaDeDestinos = [(DestinoLista.listado as AnyObject) as! String] ListaDeDestinos = DestinoLista.listado as [String]

    for key in ListaDeDestinos {

        let myString:NSString! = key as NSString

        if (myString.lowercased.contains(substring.lowercased())) {
            print(myString.contains(myString as String) ? "yep" : "nope")
            ArrayAutoComplete.append(key)

        }
    }

    mview.mostarResultados(ArrayResultados: ArrayAutoComplete) //llamamos a la función y le pasamos como parametro ArrayAutoComplete

}

}

class ViewController: UIViewController {

@IBOutlet weak var textField: UITextField! = nil
@IBOutlet weak var tableView: UITableView! = nil

var autoCompleteDestino: [String] = []

fileprivate var DestinoP: DestinoPresenter!

override func viewDidLoad() {
    super.viewDidLoad()

    //LEER: pasando como referencia tu vista
    DestinoP = DestinoPresenter(view: self)


    title = "Texto predecible"

// DestinoP.getDestinos() // DestinoP.ListarDestinos()

}
func mostarResultados(ArrayResultados: [String]) {

    autoCompleteDestino = ArrayResultados
    tableView.reloadData()

}

}

extension ViewController: UITextFieldDelegate {

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let substring = (textField.text! as NSString).replacingCharacters(in: range, with: string)

    DestinoP.searchDestinos(substring)

    return true

}

}

extension ViewController: UITableViewDataSource { // Completa el string encontrado en el tableView segun caracter ingresado en el textField public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell let index = indexPath.row as Int

    cell.textLabel!.text = autoCompleteDestino[index]

    return cell
}

}

extension ViewController: UITableViewDelegate {

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return autoCompleteDestino.count

}
//    selecciona el resultado desde el tableView para completar en el textField.
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let selectedCell: UITableViewCell = tableView.cellForRow(at: indexPath)!

    textField.text = selectedCell.textLabel!.text!

}

}

2
What are you trying to achieve?Hamish
hi! I am trying to store the result (listado) of the list in my var (ListaDeDestinos) and then show (...) If you are not understanding I can give you the complete codexhinoda
I think you need to loop through listado and pull out only the desDestino member variables. Right now you are telling the compiler to make a struct a string, which it doesn't know how to doEaston Bornemeier
A Destinos is not a String. You need some way to convert a Destinos to a String (implementing CustomStringConvertible is a popular approach, but there are many other ways to do it). The fundamental question is what you want the "string version" of a Destinos to be. You have to define that yourself. If you just want "something usable for debugging", then use String(describing:) to create string version of each element.Rob Napier

2 Answers

2
votes

If you wanted the string member variable of each listados object, do:

for object in DestinoLista.listados {
    ListaDeDestinos.append(object.desDestino)
}
2
votes

It's not clear what you want "the string version of Destinos" to be. If you don't really care, and just want "something usable for debugging," then map it to String(describing:):

let listaDeDestinos = listado.map(String.init(describing:))