Skip to content

Instantly share code, notes, and snippets.

@ryantbrown
Created March 8, 2015 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryantbrown/15220b4a5a5ed563b1af to your computer and use it in GitHub Desktop.
Save ryantbrown/15220b4a5a5ed563b1af to your computer and use it in GitHub Desktop.
Swift: Downcast AnyObject to Int
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