-1

I am trying to make the text box only take alpha numeric input only, but how do I do it in a function already without having to create another function or protocol to do so? I am only 4 weeks into learning swift, I am not sure how to use CharacterSet or Range, but I need to only take A-Z and 0-9, BUT with that input I have to be able to shift through it in that order, so if I shift it by 2 and i am on z, it will give me 1, or If i am on a and i shift by -1 it will go to 9. I have to use the protocol that I already have in place which has 2 functions, encrypt and decrypt and takes in 2 parameters, a string for the input and another string for the int to shift it by but that too takes a string, I convert that to an int to use. I am just lost on how to restrict input in the functions and shift through the indices with the int given.

protocol CipherProtocol {
func encrypt (plaintext: String, secret: String) -> String
func decrypt (output: String, secret: String) -> String
}



struct AlphanumericCesarCipher: CipherProtocol {
func encrypt(plaintext: String, secret: String) -> 
String {
    guard let secret = UInt32(secret) else {
        return "error"
    }



    var encoded = ""

    for character in plaintext {
         if let alpha = ["A","B","C","D","E","F","G","H","I","J","K","L","M",
                     "N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
                     "0","1","2","3","4","5","6","7","8","9"]  {

        }

        guard let firstUnicodeScalar = character.alpha.first else {
                return "error"
            }
            let unicode = firstUnicodeScalar.value
            let shiftedUnicode = unicode + secret
            let shiftedCharacter = String(UnicodeScalar(UInt8(shiftedUnicode))) //what does this line mean

            encoded += shiftedCharacter
        }
        return encoded
    }


func decrypt(output: String, secret: String) -> String {
    return ""
}


}
monkey
  • 33
  • 6
  • Change the keyboard type of your text field. [Programmatically change UITextField Keyboard type](https://stackoverflow.com/questions/7301018/programmatically-change-uitextfield-keyboard-type) – Torongo Feb 27 '19 at 04:22
  • @Torongo That doesn't prevent users from pasting text into the text field or using an external keyboard. Never rely on the keyboard type. – rmaddy Feb 27 '19 at 04:33
  • @monkey - You seem to be asking two completely different questions. One about allowing only certain text to be entered into a text field and one about the cipher. Please only ask one question per post. Please note that there are plenty of existing questions showing you how to allow only certain characters in a text field. Please search. Update this question to focus only on the other part. – rmaddy Feb 27 '19 at 04:35
  • @rmaddy can you please link where I can find that plenty of questions where I can find how to take in only alphanumeric input, that will only be implemented in the alphanumeric struct without creating another funciton or protocol? – monkey Feb 27 '19 at 04:43
  • Is this for iOS or macOS? – rmaddy Feb 27 '19 at 04:51
  • @rmaddy im doing this in xcode using a simulator using iphone xr – monkey Feb 27 '19 at 04:53

1 Answers1

0

I think what you need is using of 'Regular Expressions'. In this case your text input will only accept alphanumeric or any set of characters that you specify.

Try this code in your view controller to only allow a-z,A-Z,0-9 characters as input. Then you can use your logic to shift or other tasks you need.

override func viewDidLoad() {
    super.viewDidLoad()
    txt_input.delegate = self
}


func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    do {
        let text = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        let regex = try NSRegularExpression(pattern: "^([a-zA-Z0-9])$", options: [])
        if regex.firstMatch(in: text, options: [], range: NSMakeRange(0, text.count)) != nil {
            return true
        }
    }
    catch {
        print("ERROR")
    }
    return false
}
Amir.n3t
  • 2,859
  • 3
  • 21
  • 28