无法将'Swift._NSContiguousString'类型的值转换为'(Swift.String,UIImage,Swift.String)'

时间:2016-10-28 15:36:21

标签: swift3

我有一组称为服务的元组:

let services = [
    (name: "iCloud",   image: #imageLiteral(resourceName: "iCloudLogo"), url: "http://www.icloud.com"),
    (name: "Exchange", image: #imageLiteral(resourceName: "ExchangeLogo"), url: "http://www.exchange.com")
]

我有一个集合视图,用于表示此集合。我用

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let service = services[indexPath.item]

    performSegue(withIdentifier: "showAuthWindow", sender: service)
}

UICollectionViewDelegate协议捕获这些项目的点击并执行segue,将单个元组作为发送者传递。

我使用prepare for segue捕获此segue并设置目标link的{​​{1}}属性。但是,当我将viewControllersender转换回元组类型时,我收到错误:

  

无法将'Swift._NSContiguousString'类型的值转换为'(Swift.String,UIImage,Swift.String)'

Any?

请有人告诉我为什么?

1 个答案:

答案 0 :(得分:0)

发件人不是您的数据。发件人是启动segue的对象(通常是按钮或tableview或其他)。忽略发送者的标准非常标准,除非它对视图控制器产生影响(如果你点击添加按钮,目的地需要知道它在添加模式而不是编辑模式)。使用选定的索引路径来了解被点击的单元格。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showAuthWindow" {
            guard let indexPath = collectionView?.indexPathsForSelectedItems?.first else {
                return
            }
            let service = services[indexPath.item]
            let authWindow = segue.destination as! AuthorisationViewController
            authWindow.link = service.url
        }
    }