2016-12-16 3 views
0

Ich versuche, Ergebnis in Textfeld in Swift 3 auszugeben, aber wenn die Taste gedrückt wird, passiert nichts, wird nicht einmal in der Konsole gedruckt. Es sollte irgendwo in den letzten 3 Zeilen Code sein, denke ich. Ich kann nicht herausfinden, was ich falsch mache, also wird deine Hilfe sehr geschätzt! Ich bin auch neu in Swift, also könnte es etwas für dich sein, aber für mich ist es eine Sackgasse.Wie wird das Ergebnis in UITextView in Swift 3 ausgegeben?

Und dies ist mein 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) 
    } 


} 
+0

Sie rufen nicht 'encrypt()' – shallowThought

+0

Ich sehe keine UITextView in Ihrem Code. Wollen Sie das Ergebnis in Ihrem UITextView anzeigen? Sie müssen myUITextView.text = "irgendeinen Text" eingeben, wobei myUITextView Ihr UITextView-Objekt ist. – Alic

+0

Dies ist die Textansicht 'outputText.text = output' – pipp5

Antwort

1

Sie haben eine Funktion (encrypt) in einer anderen Funktion verschachtelt (die @IBAction auch encrypt genannt), aber Sie werden nie die verschachtelte Funktion aufrufen. Probieren Sie etwas wie folgt aus:

@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 

} 

Es ist auch ein wenig schwierig, genau zu bestimmen, was Sie tun, weil Sie so viele Variablen mit denselben Namen deklarieren (Text, Schlüssel, Verschlüsselung, etc.). Die Auswahl geringfügiger Variationen dieser Namen kann die Lesbarkeit Ihres Codes verbessern.

+1

Vielen Dank Nathan! Es funktionierte wie ein Charme :) Vielen Dank noch einmal für Ihre Hilfe! – pipp5

+0

Großartig! Fühlen Sie sich frei, die Antwort als "akzeptiert" zu markieren, damit auch andere in Zukunft von der Antwort profitieren können. – nathan