Error: Cannot convert value of type 'String?' to expected argument type '[Int]'

Hello all, I'm trying to display the result in a UITextField, but I'm getting the following error: Cannot convert value of type 'String?' to expected argument type '[Int]'

I'm also new to swift, and I understand little bit about the issue, but unfortunatelly not enough to fix it. So, your help will be much appreciated.


This is the code:


@IBAction func decrypt(sender: AnyObject?) {
   
        func decrypt(text: String, key:[Int]) -> String {
            let text = text.lowercased()
            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 output
        }
   
        let text = decryptText.text
        let key = pskey.text
   
        /
        let (resultText, resultKey) = decrypt(text: text!, key: key)
   
        /
        outputText.text = resultText
    }

Line 31 is where I get the error.

Where do you find your `decrypt(text:key:)` method? It seems it's using its original encryption/decryption method, which may be a simple character order conversion as found in the era of Roman empire...


You need to pass an Array of Int having the same number of element as the number of characters in the decrypted text. And the values needs to be consistent with the decypt method, with a slight mistake, the decrypt method may generate some meaningless garbage or crash your app.

Do you intend to force users to input such machine dependent numbers?


And one more, even if you have successfully converted your `pskey.text` to an Array of Int, the result of `decrypt(text:key:)` is a String, so you cannot assign it to `(resultText, resultKey)`. Maybe you have copied some code from `encrypt` part, but you should consider `what this line of code is doing?` before copying the code.


You'd better try to explain what's the background of your code.

@OOper I won't reply here as well, because you already have the answer on stackoveflow, but whet I find how to solve it, will post it here as well. May be usefull for the others.

You or any other users of the forums can write his own answer, I would not mind that.


I just have written an advice to get better solutions sooner. It's your choice if you follow my advice or not.

@OOper I misunderstood your reply completely! And I mean both places. I tried to tweak code from tutorial, whic I guess leds to more mistakes than learning. I'll abandon it for now, and go try to build something on my own, and probably post it here.


However, thank you for your help OOper!

Error: Cannot convert value of type 'String?' to expected argument type '[Int]'
 
 
Q