代码之家  ›  专栏  ›  技术社区  ›  techno

无法转换“nsStoryboardSegue.identifier”类型的值?到所需的参数类型“string”

  •  1
  • techno  · 技术社区  · 7 年前

    im使用以下代码显示视图控制器,im使用identifier属性标识segue。

    无法转换'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
            }
    
        }
    }
    

    请给我建议

    1 回复  |  直到 7 年前
        1
  •  2
  •   vadian    7 年前

    在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 { ...