I am writing an application which needs inputs of volumes and prices, I want to limit the volume to a 4 digit number, and the price to a 4 digit number with a decimal in place between the 4 digits.
How do I go about encoding this into my project?
I have searched multiple pages and found this: (http://www.globalnerdy.com/2016/05/24/a-better-way-to-program-ios-text-fields-that-have-maximum-lengths-and-accept-or-reject-specific-characters/) I still keep getting an error towards the end of my code for the TextFieldMaxLength.Swift
file.
@objc func limitLength(textField: UITextField) {
guard let prospectiveText = textField.text, prospectiveText.count > maxLength else {
return
}
// If the change in the text field's contents will exceed its maximum length,
// allow only the first [maxLength] characters of the resulting text.
let selection = selectedTextRange
text = prospectiveText.substringWith(
Range<String.Index>(prospectiveText.startIndex ..< prospectiveText(maxLength))
)
selectedTextRange = selection
}
I am expecting to be able to limit the individual textFields to different numbers. In the interface building there is a section that can be entered to limit this. But this error keeps popping up: "Cannot call value of non-function type String
", in the section which depicts:
text = prospectiveText.substringWith(
Range<String.Index>(prospectiveText.startIndex ..< prospectiveText(maxLength))
)