【问题标题】:Im getting the error = Cannot assign value of type 'String?' to type '[String]' Ive looked everywhere and i can't seem to find the answer我收到错误 = 无法分配“字符串”类型的值?输入 '[String]' 我到处找,我似乎找不到答案
【发布时间】:2016-07-05 22:11:00
【问题描述】:
import UIKit

class FirstViewController: UIViewController
 {

    @IBOutlet var Dishname: UILabel!

    @IBOutlet var TEXT: UITextField!

    @IBOutlet var recipy: UIView!



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        let secondVC: SecondViewController = segue.destinationViewController as! SecondViewController


        secondVC.items = TEXT.text! as? String    //Error

// Cannot assign value of type 'String?' to type '[String]'

    }

【问题讨论】:

  • if let text = TEXT.text { secondVC.items.append(text) }.
  • 是的,看起来 SecondViewController.items 是一个字符串数组,而您正在向它传递一个可选字符串。请为 SecondViewController 发布您的代码。我假设您正在初始化 SecondViewController.items?如果是这样,您可以按照@Eendje 的建议进行操作。

标签: arrays swift string types


【解决方案1】:

SecondViewController 中的 items 变量是 String 数组,[String] 类型,您尝试将 String 分配给它而不是插入或附加到它。

【讨论】:

  • 错误信息应该是赠品。事实上,他分配给“项目”,而不是“项目”。
猜你喜欢
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 2020-03-31
  • 1970-01-01
相关资源
最近更新 更多