从Swift 3更新项目时出现便利初始化错误

时间:2019-04-26 06:28:58

标签: ios swift xcode initialization

我正在更新一个由Swift 3制作的项目,现在出现以下错误。

  

编译器无法合理地对该表达式进行类型检查   时间;尝试将表达式分成不同的子表达式

关于该错误,有很多答案,但是我不明白如何在下面的代码中创建属性的子表达式,因为这些属性需要一起使用。

extension SJTSettings {
convenience init(width: Int? = nil,
                 height: Int? = nil,
                 videoCodec: String? = nil,
                 videoBitrate: Int? = nil,
                 keyframeInterval: Int? = nil,
                 h264Profile: String? = nil,
                 audioCodec: Int? = nil,
                 audioBitrate: Int? = nil)
{

    self.init(properties: [ // where the error warning appears
        "width": width ?? NSNull(),
        "height": height ?? NSNull(),
        "videoCodec": videoCodec ?? NSNull(),
        "videoBitrate": videoBitrate ?? NSNull(),
        "keyframeInterval": keyframeInterval ?? NSNull(),
        "h264Profile": h264Profile ?? NSNull(),
        "audioCodec": audioCodec ?? NSNull(),
        "audioBitrate": audioBitrate ?? NSNull(),
    ])
  }
}

感谢您的帮助或指导,谢谢

1 个答案:

答案 0 :(得分:1)

在传递给指定的init之前声明属性应该有助于类型推断

convenience init(width: Int? = nil,
                 height: Int? = nil,
                 videoCodec: String? = nil,
                 videoBitrate: Int? = nil,
                 keyframeInterval: Int? = nil,
                 h264Profile: String? = nil,
                 audioCodec: Int? = nil,
                 audioBitrate: Int? = nil)
{ 
    let properties: [String: Any] = [

           "width": width ?? NSNull(),
           "height": height ?? NSNull(),
           "videoCodec": videoCodec ?? NSNull(),
           "videoBitrate": videoBitrate ?? NSNull(),
           "keyframeInterval": keyframeInterval ?? NSNull(),
           "h264Profile": h264Profile ?? NSNull(),
           "audioCodec": audioCodec ?? NSNull(),
           "audioBitrate": audioBitrate ?? NSNull(),

    ]

    self.init(properties: properties)
}