When inspecting a text field by overriding the method below in swift:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
the line below returns false when I type in a 1 into the text field
var hasLeadingOne = length > 0 && decimalString.characterAtIndex(0) == (1 as unichar)
hasLeadingOne returns false but it should be true when I enter 1 into the text field. Moreover, the value of decimalString.characterAtIndex(0) seems to be 49 instead of 1 even when casted as unichar. What am I missing?
49is the ASCII character code for1.unicharis just atypealiasforUInt16. The nameunicharmakes it sound like it is a character of some kind, but it is just a number.49is the Unicode value for the character1.characterAtIndex:works in Unicode, not ASCII. Of course the first 127 ASCII is the same as the first 127 Unicode.decimalString.characterAtIndex(0) == (49 as unichar)should betrue, since49is the ASCII character code for1, as @Kenney pointed out.