首页 > 解决方案 > when trying to pass data between 2 controllers, I get the error cannot assign value of type 'ViewController.Item?' to type 'Item?',

问题描述

When using a segue, Im trying to pass the information to another controller using

(Taken from ViewController)

var itemArray: [Item] = []

on the second viewcontroller (InfoViewController) I have

var item: Item?

This is my segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "showDetail" {
        let InfoController = segue.destination as! InfoViewController
        InfoController.item = sender as? Item

Why am I getting the error when both item and itemArray are in the class Item. How do I make it so that they are both the same data type to transfer the data to InfoViewController

Full code for InfoViewController:

class InfoViewController: UIViewController {

var item: Item?


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

}

public class Item { let name: String let image: String

init(name: String, image: String) {
    self.name = name
    self.image = image

}

}

ViewController is too long (150 lines, so I have omitted the search functionality, and the search scope code.

  @IBOutlet var tableView: UITableView!
@IBOutlet var searchBar: UISearchBar!

var itemArray: [Item] = []
var currentItemArray: [Item] = []


override func viewDidLoad() {
    super.viewDidLoad()
    setUpItems()
    setUpSearchBar()
    self.searchBar.showsScopeBar = true
    searchBar.placeholder = "Search Item by Name"

}

private func setUpItems() {
    itemArray.append(Item(name: "Plastic Bottles", category: .Plastic, image: "1"))
    itemArray.append(Item(name: "Cans", category: .Metal, image: "2"))

    currentItemArray = itemArray

}

private func setUpSearchBar() {
    searchBar.delegate = self

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return currentItemArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TableViewCell
    else {
        return UITableViewCell()
    }

    cell.nameLabel.text = currentItemArray[indexPath.row].name
    cell.imgView.image = UIImage(named:currentItemArray[indexPath.row].image)

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "showDetail" {
        let InfoController = segue.destination as! InfoViewController
        InfoController.item = sender as? Item
    }

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let item = itemArray[indexPath.row]
    performSegue(withIdentifier: "showDetail", sender: item)
}

public class Item {
let name: String
let image: String
let category: ItemType

init(name: String, category: ItemType, image: String) {
    self.name = name
    self.image = image
    self.category = category
}

}

enum ItemType: String {
    case Plastic = "Plastic"
    case Metal = "Metal"
    case Paper = "Paper"

标签: swiftsegue

解决方案


Make sure you didn't declare the Item type as a member of a view controller.

Declare the Item struct or class globally in your project.


推荐阅读