Created
March 8, 2015 06:11
-
-
Save ryantbrown/15220b4a5a5ed563b1af to your computer and use it in GitHub Desktop.
Swift: Downcast AnyObject to Int
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func didReceiveCredit(creditInfo: [NSObject:AnyObject]!) { | |
// the following does not work | |
var amount: Int = creditInfo["credits"]! as Int | |
// nor does this | |
var amount: Int = Int(creditInfo["credits"]! as NSNumber) | |
// but this does | |
var amount: String = creditInfo["credits"]! as String | |
// so we can get what we want with this | |
var amount: Int = (creditInfo["credits"]! as String).toInt()! | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment