Obj-c to Swift conversion question
The more I stretch to Swift goals, the more I learn. However I've come upon a little thing where I am translating code into Swift and quickly stumbled. *Obj-C:* NSValue *keyboardEndFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; *Swift (the closest I've come):* var keyboardEndFrameValue = NSValue(notification.userInfo[UIKeyboardFrameEndUserInfoKey]) Error for Swift: insert nonretainedObject: When I Fix-It with the above suggestion, the error becomes Cannot subscript a value of type '[NSObject: AnyObject]?' with an index of type 'String' So I then end up with this (but it looks really strange): var keyboardEndFrameValue = NSValue(nonretainedObject: notification.userInfo[UIKeyboardFrameEndUserInfoKey as String]) No warnings or errors, but I wanted to check to see if I'm appeasing the compiler but have it wrong. Thanks, Eric ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Obj-c to Swift conversion question
Le 28 juil. 2015 à 16:03, Eric E. Dolecki edole...@gmail.com a écrit : The more I stretch to Swift goals, the more I learn. However I've come upon a little thing where I am translating code into Swift and quickly stumbled. *Obj-C:* NSValue *keyboardEndFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; *Swift (the closest I've come):* var keyboardEndFrameValue = NSValue(notification.userInfo[UIKeyboardFrameEndUserInfoKey]) NSValue() is not a cast, it is a constructor. Should be something like var keyboardEndFrameValue = notification.userInfo[UIKeyboardFrameEndUserInfoKey] as NSValue Error for Swift: insert nonretainedObject: When I Fix-It with the above suggestion, the error becomes Cannot subscript a value of type '[NSObject: AnyObject]?' with an index of type 'String' So I then end up with this (but it looks really strange): var keyboardEndFrameValue = NSValue(nonretainedObject: notification.userInfo[UIKeyboardFrameEndUserInfoKey as String]) No warnings or errors, but I wanted to check to see if I'm appeasing the compiler but have it wrong. Thanks, Eric ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/mailing%40xenonium.com This email sent to mail...@xenonium.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Obj-c to Swift conversion question
Yes, thanks. I ended up with this (yes, I like to use Void): func keyboardWillShow(notification:NSNotification) - Void { let userInfo = notification.userInfo! let keyboardEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() let animDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue UIView.animateWithDuration(animDuration, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { var textFieldFrame = self.ipField.frame textFieldFrame.origin.y = keyboardEndFrame.origin.y - textFieldFrame.size.height self.ipField.frame = textFieldFrame; }, completion: { _ in println(move complete.) }) } On Tue, Jul 28, 2015 at 11:35 AM Jean-Daniel Dupas mail...@xenonium.com wrote: Le 28 juil. 2015 à 16:03, Eric E. Dolecki edole...@gmail.com a écrit : The more I stretch to Swift goals, the more I learn. However I've come upon a little thing where I am translating code into Swift and quickly stumbled. *Obj-C:* NSValue *keyboardEndFrameValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey]; *Swift (the closest I've come):* var keyboardEndFrameValue = NSValue(notification.userInfo[UIKeyboardFrameEndUserInfoKey]) NSValue() is not a cast, it is a constructor. Should be something like var keyboardEndFrameValue = notification.userInfo[UIKeyboardFrameEndUserInfoKey] as NSValue Error for Swift: insert nonretainedObject: When I Fix-It with the above suggestion, the error becomes Cannot subscript a value of type '[NSObject: AnyObject]?' with an index of type 'String' So I then end up with this (but it looks really strange): var keyboardEndFrameValue = NSValue(nonretainedObject: notification.userInfo[UIKeyboardFrameEndUserInfoKey as String]) No warnings or errors, but I wanted to check to see if I'm appeasing the compiler but have it wrong. Thanks, Eric ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/mailing%40xenonium.com This email sent to mail...@xenonium.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com