pipp5
pipp5

Reputation: 5

How to output result in UITextView in Swift 3?

I'm trying to output result in text field in Swift 3, but when the button is pressed nothing happens, doesn't even print in the console. It should be somewhere in the last 3 lines of code I guess. I can't figure out what I'm doing wrong, so your help is much appreciated! I'm also new to Swift, so it may be something obvious to you, but dead end for me.

And this is my code:

@IBAction func encrypt(sender: AnyObject?) {

    let text = encryptText.text
    let key = pkey.text

    func encrypt(text: String) -> (text: String, key: [Int]) {
        let text = text.lowercased()
        let key = self.key(count: text.characters.count)
        let map = self.map()
        var output = String()

        for (index, character) in text.characters.enumerated() {
            if character == " " {
                output.append(character)
            }

            else {
                if let letterIndex = map.forward[String(character)] {
                    let keyIndex = key[index]
                    let outputIndex = (letterIndex + keyIndex + map.lastCharacterIndex) % map.lastCharacterIndex
                    if let outputCharacter = map.reversed[outputIndex] {
                        output.append(outputCharacter)
                    }
                }
            }
        }
        print(text)
        outputText.text = output
        return (text: output.uppercased(), key: key)
    }


}

Upvotes: 0

Views: 655

Answers (1)

nathangitter
nathangitter

Reputation: 9777

You have a function (encrypt) nested in another function (the @IBAction also called encrypt), but you are never calling the nested function. Try something like this:

@IBAction func encrypt(sender: AnyObject?) {

    func encrypt(text: String) -> (text: String, key: [Int]) {
        let text = text.lowercased()
        let key = self.key(count: text.characters.count)
        let map = self.map()
        var output = String()

        for (index, character) in text.characters.enumerated() {
            if character == " " {
                output.append(character)
            }

            else {
                if let letterIndex = map.forward[String(character)] {
                    let keyIndex = key[index]
                    let outputIndex = (letterIndex + keyIndex + map.lastCharacterIndex) % map.lastCharacterIndex
                    if let outputCharacter = map.reversed[outputIndex] {
                        output.append(outputCharacter)
                    }
                }
            }
        }
        return (text: output.uppercased(), key: key)
    }

    let text = encryptText.text
    let key = pkey.text

    // call the encrypt function
    let (resultText, resultKey) = encrypt(text: text)

    // put the result in the text view
    outputText.text = resultText

}

It's also a little difficult to determine exactly what you are doing because you declare so many variables with the same names (text, key, encrypt, etc). Choosing slight variations of those names can improve the readability of your code.

Upvotes: 1

Related Questions