【问题标题】:Initializer for conditional binding must have Optional type, not 'CGFloat'条件绑定的初始化程序必须具有可选类型,而不是“CGFloat”
【发布时间】:2017-04-08 08:58:28
【问题描述】:

有两个错误我被卡住了。当我在新的 xcode 8 swift 3.0 中打开我的项目时遇到这个错误我不知道如何纠正这个错误。我已经解决了其他一些错误。但是由于上述错误而在此行中敲击。

 func keyboardWillShow(_ notification: Notification) {
        keyboardHasBeenShown = true

        guard let userInfo = (notification as NSNotification).userInfo else {return}
        guard let endKeyBoardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.minY else {return}

        if tmpContentViewFrameOrigin == nil {
        tmpContentViewFrameOrigin = self.contentView.frame.origin
        }

        if tmpCircleViewFrameOrigin == nil {
        tmpCircleViewFrameOrigin = self.circleBG.frame.origin
        }

        var newContentViewFrameY = self.contentView.frame.maxY - endKeyBoardFrame
        if newContentViewFrameY < 0 {
            newContentViewFrameY = 0
        }
        let newBallViewFrameY = self.circleBG.frame.origin.y - newContentViewFrameY
        self.contentView.frame.origin.y -= newContentViewFrameY
        self.circleBG.frame.origin.y = newBallViewFrameY
    }

在上述方法中:

我的错误在下面一行:

guard let endKeyBoardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.minY else {return}

错误:

Initializer for conditional binding must have Optional type, not 'CGFloat'

以下方法的第二个错误:

open func showCustom(_ title: String, subTitle: String, color: UIColor, icon: UIImage, closeButtonTitle:String?=nil, duration:TimeInterval=0.0, colorStyle: UInt=SCLAlertViewStyle.success.defaultColorInt, colorTextButton: UInt=0xFFFFFF, circleIconImage: UIImage? = nil, animationStyle: SCLAnimationStyle = .topToBottom) -> SCLAlertViewResponder {


        var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0

        color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

        var colorAsUInt32 : UInt32 = 0
        colorAsUInt32 += UInt32(red * 255.0) << 16 + UInt32(green * 255.0) << 8 + UInt32(blue * 255.0)

        let colorAsUInt = UInt(colorAsUInt32)

        return showTitle(title, subTitle: subTitle, duration: duration, completeText:closeButtonTitle, style: .success, colorStyle: colorAsUInt, colorTextButton: colorTextButton, circleIconImage: icon, animationStyle: animationStyle)
    }

此行错误:

 colorAsUInt32 += UInt32(red * 255.0) << 16 + UInt32(green * 255.0) << 8 + UInt32(blue * 255.0)

错误:

表达式过于复杂,无法在合理时间内解决;考虑将表达式分解为不同的子表达式

【问题讨论】:

标签: ios swift


【解决方案1】:

需要加问号:

guard let endKeyBoardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? AnyObject).cgRectValue.minY else {return}

colorAsUInt32 += UInt32(red * 255.0) << 16
colorAsUInt32 += UInt32(green * 255.0) << 8
colorAsUInt32 += UInt32(blue * 255.0)

【讨论】:

  • @mack 拆分表达式。​​
  • 我只在 5 天前才接触 ios。这个项目被我的老开发者打动了。所以我不知道如何为此做出分手表达。
  • @mack 考虑将表达式分解为不同的子表达式。 colorAsUInt32 += UInt32(red * 255.0)
  • 如果我喜欢这个 `colorAsUInt32 += UInt32(red * 255.0)
  • plus for colorAsUInt32。
猜你喜欢
  • 2019-03-11
  • 2017-01-22
  • 2018-03-25
  • 2016-01-08
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
相关资源
最近更新 更多