无法指定类型'[ViewController.Variable_Name]'的值来键入'[String]'

时间:2017-11-22 21:02:54

标签: ios swift

我正在尝试将数组传递给另一个UIViewController但是会​​收到此错误:

Cannot assign value of type '[ViewController.Lugares]' to type '[String]'

这段代码

    if(Array[0]=="Lahua Park"){
       //send me to that location 
    }

First View Controller:

import UIKit

class ViewController: UIViewController, UITableViewDelegate,  UITableViewDataSource {

    ...

    var fetchedCountry = [Lugares]()

    class Lugares{
        var lugares : String

        init(lugares: String){
            self.lugares = lugares
        }
    }

    ...

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "segue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let secondController = segue.destination as! SecondController

        secondController.transferPaises = fetchedCountry (Error Ocurrs)
    }

Second View Contoller:

class SecondController: UIViewController {

var transferPaises = Array<String>()

..

override func viewDidLoad() {
    super.viewDidLoad()


    if (transferPaises[0]=="Mexico") {
        print(transferPaises)

            location.latitude = 19.432608
            location.longitude = -99.133209
            let region = MKCoordinateRegion(center: location, span: span)

            map.setRegion(region, animated: true)

        }

    ....

    }
}

2 个答案:

答案 0 :(得分:0)

transferPaises是一个字符串数组

var transferPaises = Array<String>()

fetchedCountry NOT 一个字符串数组:

var fetchedCountry = [Lugares]()

因此,您无法将类型Lugares的数组分配给String类型的数组

答案 1 :(得分:0)

将此作为在UIViewController

之外宣布的公共类
public class Lugares{
        var lugares : String

        init(lugares: String){
            self.lugares = lugares
        }
}

另外,将transferPaises更改为匹配fetchedCountry类型

var transferPaises = [Lugares]()