swift - 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"
解决方案
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.
推荐阅读
- c# - 忽略 ASP.NET MVC WebAPI 中的路由参数
- ios - 如何在 XCode 的 IB 中使用 Material 框架?
- angular - 如何将值从对话服务传递到父 Angular 2
- python - Python:如何并行化用于具有许多函数输入的嵌套 for 循环中的函数?
- dependency-injection - 如何在责任链模式中使用 Unity IoC 注入多个不同的依赖项?
- google-analytics - 将自定义视图保存为默认值
- python - 如何在 Python 中使用 GLPK 为目标函数设置时间限制或松弛参数?
- typescript - Typescript 泛型:给定键 K 和对象 T,约束 T[K] 的类型
- excel - 在一列中查找类似日期然后计算最小值/最大值之间的差异的公式
- android - Android:使用 Viewmodel 缓存来自 EditText 的瞬态 UI 数据的最佳方式