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 ""
}
}