簡體   English   中英

無法在強制轉換中將[Struct]類型的值轉換為[string]類型

[英]cannot convert value of type [Struct] to type [string] in coercion swift

我收到此錯誤:

無法在強制轉換中將[Destinos]類型的值轉換為[String]類型

我有這個結構:

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

然后:

var ListaDeDestinos = [String]()

所以我的錯誤出現在這一行:

ListaDeDestinos = DestinoLista.listado as [String]

怎么了 能幫我嗎? 我在論壇上找不到這樣的東西

編輯我所有的代碼:

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)一樣! [String] ListaDeDestinos = DestinoLista.listado為[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

}

}

類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()

}

}

擴展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

}

}

擴展ViewController:UITableViewDataSource {//完成字符串綁定,並完成tableView腳本的創建,並完成textField公共函數tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier:“ cell” ,用於:indexPath)作為UITableViewCell,讓index = indexPath.row作為Int

    cell.textLabel!.text = autoCompleteDestino[index]

    return cell
}

}

擴展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的字符串版本”是什么。 如果您不太在意,只想“對調試有用”,則將其映射到String(describing:)

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

如果要每個listados對象的字符串成員變量,請執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM