Search code examples
macosstoryboardsegueswift4

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


Im using the following code to show a view controller,im identifying the segue using the identifier property.This code works fine in swift3 but when updating to swift4 I get the following error

Cannot convert value of type 'NSStoryboardSegue.Identifier?' to expected argument type '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
        }

    }
}

Please advice


Solution

  • In Swift 4 the type of the segue identifier has been changed to NSStoryboardSegue.Identifier

    Two solutions

    1. Compare the rawValue – and safely unwrap the identifier

      if let identifier = segue.identifier, identifier.rawValue == "segue" { ...
      
    2. (recommended) Create an extension

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

      and compare

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