Skip to content

Instantly share code, notes, and snippets.

@tatimagdalena
Created October 7, 2016 15:08
Show Gist options
  • Save tatimagdalena/9876cfb7e64538495d8e77c5bd61e5b1 to your computer and use it in GitHub Desktop.
Save tatimagdalena/9876cfb7e64538495d8e77c5bd61e5b1 to your computer and use it in GitHub Desktop.
func convertToValidInt(amount: String) -> Int?
if amount != "" {
let badCharacters = CharacterSet.decimalDigits.inverted
if amount.rangeOfCharacter(from: badCharacters) == nil {
if let amount = Int(amount) {
if amount > 0 {
return amount
}
}
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment