I have implemented the Caesar Cipher in Swift and incorporated it into an iOS-app.
I guess it's formally correct. Please see the attached images, which show how the usage of the app is meant.
Here's the relevant code:
struct ContentView: View {
    @State var input = ""
    @State var output = ""
    @State var shift  = 1
    @State var hasSubmitted = false
    
    var body: some View {
        VStack {
            Text("Caesar Cipher").font(.title)
            Form {
                Section("Text to Encrypt/Decrypt") {
                    TextField("Text to Encrypt/Decrypt", text: $input)
                        .lineLimit(2)
                        .disabled(hasSubmitted)
                        .textInputAutocapitalization(.never)
                }
                
                Section("Shift") {
                    Picker("Digits", selection: $shift) {
                        ForEach(1...25, id: \.self) { digit in
                            Text("\(digit)")
                        }
                    }
                }
                
                Section("Submit") {
                    HStack {
                        if hasSubmitted {
                            Spacer()
                            Button("Reset") {
                                input = ""
                                output = ""
                                hasSubmitted = false
                                shift = 1
                            }.buttonStyle(.borderedProminent)
                            Spacer()
                        } else {
                            Spacer()
                            ChoiceButton(caption: "Encrypt",
                                         input: $input,
                                         output: $output,
                                         hasSubmitted: $hasSubmitted) { index, count in
                                (index + shift) % count
                            }
                            Spacer()
                            ChoiceButton(caption: "Decrypt",
                                         input: $input,
                                         output: $output,
                                         hasSubmitted: $hasSubmitted) { index, count in
                                ((index - shift) + count) % count
                            }
                            Spacer()
                        }
                    }
                }
                
                Section("Encrypt/Decrypt Result") {
                    Text(output).bold()
                }
            }
        }
        .padding()
    }
}
struct ChoiceButton: View {
    var caption: String
    let alphabet = ["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"]
    @Binding var input: String
    @Binding var output: String
    @Binding var hasSubmitted: Bool
    var algo: (Int, Int) -> Int
    
    var body: some View {
        Button(caption) {
            guard input.isEmpty == false else {
                return
            }
            
            let lInput = input.lowercased()
            hasSubmitted = true
            output = ""
            
            for char in lInput {
                let index = alphabet.firstIndex(of: String(char))
                
                if let index = index {
                    let shiftedIndex = algo(index, alphabet.count)
                    output = "\(output)\(alphabet[shiftedIndex])"
                } else {
                    output = "\(output)\(char)"
                }
            }
        }.buttonStyle(.bordered)
    }
}
Any feedback according the naming, form, algorithm, closure-usage, state-handling, app-implementation in general is welcomed.
Looking forward to reading your comments and answers.




