How do I set a textfield (Xcode 7.0 beta 3, swift 2) to numeric entries of a certain length?

I'm trying to limit 3 different textfields so that you can only enter numbers into them, and so that the number of digits you can enter is limited.

Here's my code:


import Foundation
import UIKit
import Darwin

class View3on3 : UIViewController, UITextFieldDelegate {

@IBOutlet weak var APTeams: UITextField!
@IBOutlet weak var APRounds: UITextField!
@IBOutlet weak var APBreakers: UITextField!

override func viewDidLoad()
{
    super.viewDidLoad()
    initializeTextFields()
}

func initializeTextFields()
{
    APTeams.delegate = self
    APTeams.keyboardType = UIKeyboardType.NumberPad


    APRounds.delegate = self
    APRounds.keyboardType = UIKeyboardType.NumberPad


    APBreakers.delegate = self
    APBreakers.keyboardType = UIKeyboardType.NumberPad
}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
}


@IBAction func userTappedBackground(sender: AnyObject)
{
    view.endEditing(true)
}


func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    if string.characters.count == 0 {
        return true
    }


    let prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: String())


    switch textField {
    case APTeams:
        return prospectiveText.containsOnlyCharactersIn("0123456789") && count(prospectiveText) <= 3


    case APRounds:
        return prospectiveText.containsOnlyCharactersIn("0123456789") && count(prospectiveText) <= 1


    case APBreakers:
        return prospectiveText.containsOnlyCharactersIn("0123456789") && count(prospectiveText) <= 3


    default:
        return true
    }
}
}


It throw an error at this line


let prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: String())


Saying that 'String?' is not convertible to NSString


I followed the tutorial at this site: http://www.globalnerdy.com/2015/04/27/how-to-program-an-ios-text-field-that-takes-only-numeric-input-or-specific-characters-with-a-maximum-length/


What am I doing wrong?


PS. The code to call a NumberPad is working fine. When I remove the code to limit the number of digits, it works fine. I'd also welcome any other improvements upon the code.

Saying that 'String?' is not convertible to NSString

The immediate cause of this error is that

textField.text
is optional (it's of type
NSString?
) and thus can't be converted directly to NSString.

I'll note that UITextField's

text
property is often
nil
when the text field is empty, so you do have to deal with this. Probably the easiest solution is to treat
nil
as the empty string here. For example:
let text = textField.text ?? ""
let prospectiveText = (text as NSString).stringByReplacingCharactersInRange(range, withString: String())

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

You asked for other improvements so here are some options.


Since the APTeams and APBreakers seem to have the same test, you can combine the cases APTeams and APBreakers into one case line as:


  switch textField {
  case APTeams, APBreakers:
       return prospectiveText.containsOnlyCharactersIn("0123456789") && count(prospectiveText) <= 3
  case APRounds:
       return prospectiveText.containsOnlyCharactersIn("0123456789") && count(prospectiveText) <= 1
  default:
       return true
  }


Alternatively, you can eliminate the switch statement entirely by creating a separate class for the text field delegate which takes the number of allowed digits as an argument to the constructor. Then you can assign each text field a separate instance to handle the text validation. Of course, you would have to keep a strong reference to those delegates in your view controller.

The line of code you recommended doesn't work for me.


In the switch statements, the return line -

return prospectiveText.containsOnlyCharactersIn("0123456789.") && count(prospectiveText) <= 3

I'm getting the error for the ** .containsOnlyCharactersIn **

Is there another way to call the allowed characters?

Thanks.

How do I set a textfield (Xcode 7.0 beta 3, swift 2) to numeric entries of a certain length?
 
 
Q