简体   繁体   English

prepareForSegue方法中出现错误“无法分配类型的值”(Swift,Xcode 6)

[英]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.

相关问题 Swift:无法为“ int”类型的值分配“ int”类型的值? 错误 - Swift: Cannot assign a value of type 'int' to a value of type 'int?' Error Swift3错误:无法将类型&#39;()&#39;的值分配给类型[[customObject]? - Swift3 error: Cannot assign value of type '()' to type '[customObject]?' swift无法将类型“ UITableViewCell”的值分配为类型“…Cell”的错误 - Cannot assign value of type 'UITableViewCell' to type '…Cell' error in swift 错误:无法将类型“[String]”的值分配给 swift 中的“String”类型 - Error: Cannot assign value of type '[String]' to type 'String' in swift Swift Xcode的两个错误:无法将类型“ ShapeType”的值分配给类型“ ShapeButton.ShapeType” - Swift xcode two errors: Cannot assign value of type 'ShapeType' to type “ShapeButton.ShapeType” Xcode 7.2.1 Swift 2.1.1无法将类型&#39;Dictionary &lt;_,_&gt;&#39;的值分配给&#39;Dictionary&#39;类型 - Xcode 7.2.1 Swift 2.1.1 cannot assign value of type 'Dictionary<_,_>' to type 'Dictionary' Swift不能指定类型'()'的值来键入'String?' - Swift cannot assign value of type '()' to type 'String?' Swift Xcode错误“无法下标&#39;[ListOfAnimals]类型的值” - Swift xcode error "cannot subscript a value of type '[ListOfAnimals]' SWIFT:无法分配类型…/ SwiftyJSON的值 - SWIFT : Cannot assign value of type … / SwiftyJSON Swift无法分配&#39;UINavigationController&#39;类型的值 - Swift cannot assign value of type 'UINavigationController'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM