【问题标题】:Cannot convert value of type 'NSStoryboardSegue.Identifier?' to expected argument type 'String'无法转换“NSStoryboardSegue.Identifier”类型的值?到预期的参数类型“字符串”
【发布时间】:2018-11-08 20:40:03
【问题描述】:

我使用以下代码显示视图控制器,我使用标识符属性识别 segue。此代码在 swift3 中运行良好,但在更新到 swift4 时出现以下错误

无法转换“NSStoryboardSegue.Identifier”类型的值?到预期的参数类型“字符串”

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
        }

    }
}

请指教

【问题讨论】:

    标签: macos storyboard segue swift4


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      相关资源
      最近更新 更多