有条件绑定的初始化必须有可选类型,而不是'CGFloat'
问题描述:
有两个错误我卡住了。我得到这个错误,当我打开我的项目在新的xcode 8 swift 3.0我不知道如何纠正这个错误。我已经解决了一些其他的错误。但是为了上面提到的错误,在这里发现了这个问题。有条件绑定的初始化必须有可选类型,而不是'CGFloat'
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)
错误:
表达过于复杂,在合理的时间内得到解决;考虑分手的表达成不同的子表达式
答
需要加问号:
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)
的可能的复制[条件结合初始化程序必须有可选的类型,而不是“字符串”(HTTP://计算器.com/questions/32768274/initializer-for-conditional-binding-must-have-optional-type-not-string) –
看看http://stackoverflow.com/questions/25451001/getting-keyboard-size-从swift 2 + 3代码的userinfo-in-swift中获取通知的键盘大小。 –
请不要在一个问题中发布两个完全无关的问题。 –