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!
}
}
Destinos
is not aString
. You need some way to convert aDestinos
to aString
(implementingCustomStringConvertible
is a popular approach, but there are many other ways to do it). The fundamental question is what you want the "string version" of aDestinos
to be. You have to define that yourself. If you just want "something usable for debugging", then useString(describing:)
to create string version of each element. – Rob Napier