0

Sometimes I feel so lost in the world of iOS code. It's telling me it's not unwrapped it should have a "!". When I fix it, it's telling me it's wrong and to delete the "!". So I keep going in a loop. I can't for the life of me figure out what is wrong with this piece of code:

let keyboardSize: CGSize = info.objectForKey(UIKeyboardFrameEndUserInfoKey)?.frame.size

Here is the full code:

func keyboardWasShown(notification: NSNotification) -> Void {

    let info: NSDictionary = notification.userInfo!

    let keyboardSize: CGSize = info.objectForKey(UIKeyboardFrameEndUserInfoKey)?.frame.size

    let buttonOrigin: CGPoint = self.clearAllButton.frame.origin

    let buttonHeight: CGFloat = self.clearAllButton.frame.size.height

    let visibleRect: CGRect = self.view.frame

    visibleRect.size.height -= CGFloat(keyboardSize.height) as CGFloat

    if (!CGRectContainsPoint(visibleRect, buttonOrigin)){

        let scrollPoint: CGPoint = CGPointMake(0.0, buttonOrigin.y - visibleRect.size.height + buttonHeight)

        self.scrollView.setContentOffset(scrollPoint, animated: true)

    }


}
Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • Which version of XCode are you using ? it works on XCode 6.1.1 and you should write `let keyboardSize: CGSize = info.objectForKey(UIKeyboardFrameEndUserInfoKey)!.frame.size` – Audrey Sobgou Zebaze Feb 12 '15 at 17:20
  • I'm on 6.1.1, but I've finally figured out the errors, thank you! – Lavvo Feb 12 '15 at 17:27

1 Answers1

1

Your keyboardSize constant has type of CGSize and value that you are giving to it info.objectForKey(UIKeyboardFrameEndUserInfoKey)?.frame.size may return nil so you must declare your constant type as CGSize optional let keyboardSize: CGSize? = ...

EDITED

Since you are using keyboard size for calculating visible rect you full code should be like the following

func keyboardWasShown(notification: NSNotification) -> Void {

let info: NSDictionary = notification.userInfo!

if let keyboardSize: CGSize = info.objectForKey(UIKeyboardFrameEndUserInfoKey)?.frame.size {

    let buttonOrigin: CGPoint = self.clearAllButton.frame.origin

    let buttonHeight: CGFloat = self.clearAllButton.frame.size.height

    let visibleRect: CGRect = self.view.frame

    visibleRect.size.height -= CGFloat(keyboardSize.height) as CGFloat

    if (!CGRectContainsPoint(visibleRect, buttonOrigin)){

        let scrollPoint: CGPoint = CGPointMake(0.0, buttonOrigin.y - visibleRect.size.height + buttonHeight)

        self.scrollView.setContentOffset(scrollPoint, animated: true)

    }

    }

}
Zell B.
  • 10,266
  • 3
  • 40
  • 49
  • Thank you! That fixed that line. Now this line is giving me an issue: visibleRect.size.height -= CGFloat(keyboardSize.height) as CGFloat I had it before as visibleRect.size.height = keyboardSize.height but that wasn't working either. – Lavvo Feb 12 '15 at 17:18
  • never mind, I figure out the next issue. Thank you! Sometimes, the XCode errors just throw you off. – Lavvo Feb 12 '15 at 17:26
  • I just tried to edited solution and now I am able to run it, but it is crashing at the IF - LET line, saying fatal error: unexpectedly found nil while unwrapping an Optional value. It's as if it is not detecting any keyboard and is returning nil. Also wasn't that the point of the IF-LET so that if NIL is found it should not crash....now I'm confused. – Lavvo Feb 12 '15 at 17:38
  • Are you sure that info constant is not nil because you are forcing it to unwrap. Can you try to optionally unwrap it as well ? – Zell B. Feb 12 '15 at 17:55
  • If by optional unwrap, you mean like this -> let info: NSDictionary? = notification.userInfo? I just tried that and it is still crashing at the IF-LET saying nil found. – Lavvo Feb 12 '15 at 18:06
  • With optional unwrap i meant this: if let info: NSDictionary = notification.userInfo{ // paste following code } – Zell B. Feb 12 '15 at 18:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70811/discussion-between-zellb-and-lavvo). – Zell B. Feb 12 '15 at 18:16