0

I am trying to get the Keyboard height in swift 3 however I receive the following error,

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

for my guard let statement in the following

func keyboardWillShow(_ n:Notification) {

 self.keyboardShowing = true

 guard let keyboardHeight = (((n as NSNotification).userInfo! as NSDictionary).object(forKey: UIKeyboardFrameBeginUserInfoKey) as AnyObject).cgRectValue.size.height else {
   return
 }

}

So I am returning the CGFloat value of the keyboard height for the keyboardWillShow notification. So of course I know it will always return a value when it appears but seems since Swift 3 I have to return an option just in case.

How can I change the guard let statement for swift 3 ?

A.Roe
  • 973
  • 3
  • 15
  • 34
  • 1
    The linked-to Q&A has answers for Swift 2 and Swift 3. – Martin R Sep 05 '16 at 17:17
  • You can only use `guard let` and `if let` with optional types. But, because of how you've constructed that expression, it's not an optional. – Rob Sep 05 '16 at 17:19

0 Answers0