2017-06-14 57 views
0

我得到这个错误:不能转换类型的值结构]输入[字符串]在威逼迅速

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

我有这样的结构:

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

这:

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

then:

var ListaDeDestinos = [String]() 

所以我的错误出现在该行:

ListaDeDestinos = DestinoLista.listado as [String] 

这里有什么问题?可以帮助我吗? i'm鸵鸟政策发现这样的事情在论坛

编辑 我的所有代码:

import UIKit 

类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) 

} 

}

类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!字符串] ListaDeDestinos = DestinoLista.listado如[字符串]

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 

} 

}

类的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() 

} 

}

扩展视图控制器: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 

} 

}

扩展视图控制器:UITableViewDataSource {// EL Completa串encontrado EN EL的tableView托里奥拉卡拉科特ingresado EN EL文本框 公共FUNC的tableView(_的tableView: UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {let} cell = tableView.dequeueReusableCell(withIdentifier:“cell”,for:indexPath)as UITableViewCell let index = indexPath。行作为诠释

cell.textLabel!.text = autoCompleteDestino[index] 

    return cell 
} 

}

扩展视图控制器:{的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! 

} 

}

+0

你想达到什么目的? – Hamish

+0

嗨! 我想在我的var(ListaDeDestinos)中存储列表的结果(listado),然后显示(...) 如果你不明白我可以给你完整的代码 – xhinoda

+0

我认为你需要循环listado并只提取desDestino成员变量。现在你要告诉编译器做一个结构字符串,它不知道怎么做 –

回答

1

如果你想在每一listados对象的字符串成员变量,这样做:

for object in DestinoLista.listados { 
    ListaDeDestinos.append(object.desDestino) 
} 
+0

是.. !!这工作!对于DestinoLista.listado中的对象{ ListaDeDestinos.append(object.desDestino!) } – xhinoda

1

你想成为什么 “的Destinos字符串版本” 目前尚不清楚。如果你真的不关心,只想“可用的东西进行调试”,那么其映射到String(describing:)

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

是的..这也是工作! – xhinoda

相关问题