         // Look for the next valid number after the cursor, when found return a CursorPosition struct
         for i in cursorEnd ..< textAsNSString.length  {
             let cursorRange = NSMakeRange(i, 1)
-            let candidateNumberAfterCursor = textAsNSString.substring(with: cursorRange)
-            if candidateNumberAfterCursor.rangeOfCharacter(from: nonNumericSet) != nil {
+            let candidateNumberAfterCursor: NSString = textAsNSString.substring(with: cursorRange) as NSString
+            if (candidateNumberAfterCursor.rangeOfCharacter(from: nonNumericSet as CharacterSet).location == NSNotFound) {
                 for j in cursorRange.location ..< textAsNSString.length  {
                     let candidateCharacter = textAsNSString.substring(with: NSMakeRange(j, 1))
-                    if candidateCharacter == candidateNumberAfterCursor {
+                    if candidateCharacter == candidateNumberAfterCursor as String {
                         repetitionCountFromEnd += 1
                     }
                 }

     - returns: UITextfield
     */
    required public init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.setup()
    }
    
    func setup(){
        self.partialFormatter = PartialFormatter(phoneNumberKit: phoneNumberKit, defaultRegion: PhoneNumberKit.defaultRegionCode(), withPrefix: withPrefix)
        self.autocorrectionType = .no
        self.keyboardType = UIKeyboardType.phonePad
        super.delegate = self
    }

    
    // MARK: Phone number formatting
    
    /**
    *  To keep the cursor position, we find the character immediately after the cursor and count the number of times it repeats in the remaining string as this will remain constant in every kind of editing.
    */
    
    internal struct CursorPosition {
        let numberAfterCursor: String

            }
        }
    }
    
    
    let partialFormatter: PartialFormatter
    
    let nonNumericSet: NSCharacterSet = {
        var mutableSet = NSMutableCharacterSet.decimalDigit().inverted
        mutableSet.remove(charactersIn: PhoneNumberConstants.plusChars)
        return mutableSet as NSCharacterSet
    }()
    
    weak private var _delegate: UITextFieldDelegate?
