无法将类型 'NSStoryboardSegue.Identifier?' 的值转换为预期的参数类型 'String'

Cannot convert value of type 'NSStoryboardSegue.Identifier?' to expected argument type 'String'

我使用以下代码来显示视图控制器,我使用标识符 property.This 识别 segue 代码在 swift3 中工作正常但是当更新到 swift4 时我收到以下错误

无法将类型 'NSStoryboardSegue.Identifier?' 的值转换为预期的参数类型 'String'

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    if (segue.identifier == "segue") {
        //get a reference to the destination view controller
        let destinationVC:ProgressView = segue.destinationController as! ProgressView

        //set properties on the destination view controller
        destinationVC.fileArray=fileArray
        destinationVC.croptype=croptype
        destinationVC.outdir=outdir
        destinationVC.fileformat=fileformat
        destinationVC.tflag=tflag
        if(resize==true)
        {
        destinationVC.resize=true
        destinationVC.rwidth=rwidth
        destinationVC.rheight=rheight
        destinationVC.preserve_aspect_ratio=preserve_aspect_ratio
        }

    }
}

请指教

在 Swift 4 中,segue 标识符的类型已更改为 NSStoryboardSegue.Identifier

两种解决方案

  1. 比较 rawValue – 并安全地打开标识符

    if let identifier = segue.identifier, identifier.rawValue == "segue" { ...
    
  2. (推荐)创建扩展

    extension NSStoryboardSegue.Identifier {
        static let segue = NSStoryboardSegue.Identifier("segue")
    }
    

    并比较

    if let identifier = segue.identifier, identifier == .segue { ...