[英]Error “cannot assign a value of type” in prepareForSegue method (Swift, Xcode 6)
I just want to make a segue by an example here http://www.codingexplorer.com/segue-uitableviewcell-taps-swift/ 我只是想在这里http://www.codingexplorer.com/segue-uitableviewcell-taps-swift/
But when I made this code: 但是当我编写这段代码时:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == blogSegueIdentifier {
if let destination = segue.destinationViewController as? specialitiesViewController {
if let cellIndex = tableView.indexPathForSelectedRow()?.row {
destination.formuleName = formulesList[cellIndex]
}
}
}
}
Appeared error called: 出现的错误称为:
cannot assign a value of type '(Formules)' to a value of type String" 无法将类型“(Formules)”的值分配给字符串类型的值”
this is a class which contents a data for formuleList
这是一个包含formuleList
数据的formuleList
formuleList
is an array with a list of cell names formuleList
是具有单元名称列表的数组
how to remove error? 如何清除错误? Thank u for your help. 感谢您的帮助。
The way that this segue block works is you are basically telling the UI that I'm going to segue to the next view controller 这个segue块的工作方式是,您基本上是在告诉UI我要选择到下一个视图控制器
if let destination = segue.destinationViewController as? specialitiesViewController
if let destination = segue.destinationViewController as? specialitiesViewController
- says that the ViewController
we are going to is of custom type specialitiesViewController
if let destination = segue.destinationViewController as? specialitiesViewController
表示我们要使用的ViewController
具有自定义类型specialitiesViewController
My assumption would be that in your specialitiesViewController
you have a variable called forumlaName
that is of type String
and that your formulesList
is of type [Formules]
- and as such this assignment is invalid. 我的假设是,在您的specialitiesViewController
您有一个名为forumlaName
的变量,其类型为String
,而您的formulesList
的类型为[Formules]
-因此,此分配无效。
You are trying to assign a specific Formules
to a String
and thus occurs your error. 您试图将特定的Formules
分配给String
,从而发生错误。
Fix choices would be to make sure the type Formules
implements one of the String protocols such as Printable
that allows it to be represented as a string. 修复选择是确保Formules
类型实现String协议之一,例如Printable
,以允许将其表示为字符串。 Alternativley perhaps you have something like a name
variable inside that is a string Alternativley也许您内部有一个像字符串一样的name
变量
Depending on the version of Swift you are using you could do something like: 根据您使用的Swift版本,您可以执行以下操作:
extension Formules : Printable {
override public var description: String {
return "TEST"
}
}
but with out more actual code info i can't tell you if this will actually work 但是没有更多实际的代码信息,我无法告诉您这是否真的有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.