I'm using this piece of code to limit user input regarding the keyboard.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let allowedCharacters = CharacterSet.init(charactersIn: ".0123456789")
let characterSet = NSCharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet as CharacterSet)
}
but my program crashes when I enter any character.

Update 2: So 99.9% of this solution works great, unfortunately the period/decimal point does not register. Unsure why this is happening?
This appears to be a bug in Swift, there are multiple issues on this matter on the Swift issue tracker: https://bugs.swift.org/browse/SR-3311, https://bugs.swift.org/browse/SR-3667
Until this has been fixed, you can workaround this problem by using the following extension:
Keep in mind that you need to change your
characterSetvariable from typeNSCharacterSettoCharacterSetto be able to use that extension in your example.