2017-01-30 2 views
0

최근에 Swift 3 구문을 배웠고 좋은 첫 번째 프로젝트는 Vigenère Cipher라고 생각했습니다. 그래서 나는 놀이터에서 그것을위한 스크립트를 만들기 시작했습니다.잘못된 명령어 오류 Swift 3 및 XCode 8

문제는 메서드를 호출 할 때 오류가 계속 발생하며 실수가 무엇인지 알기 때문에 내 사전 값과 해당 값을 래핑하지 않는 사실과 관련이 있습니다. 그러나 나는 그것에 대해 무엇을 해야할지 알지 못합니다. 어떤 아이디어?

import Foundation 


let alphabet: [String: Int] = ["a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, 
          "j": 9, "k": 10, "l": 11, "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, 
          "r": 17, "s": 18,"t": 19, "u": 20, "v": 21, "w": 22, "x": 23, "y": 24, "z": 25] 

let alphabet2: [Int: String] = [0: "a", 1: "b", 2: "c", 3: "d", 4: "e", 5: "f", 6: "g", 7: "h", 8: "i", 
          9: "j", 10: "k", 11: "l", 12: "m", 13: "n", 14: "o", 15: "p", 16: "q", 
          17: "r", 18: "s", 19: "t", 20: "u", 21: "v", 22: "w", 23: "x", 24: "y", 25: "z"] 


var mess = "I once saw a big mosquito" 
var key = "pass" 


func cipher(message: String, key: String) -> String{ 
    var code: [Int] = [] // will hold the encripted code 

    // removes whietspace from message and key 
    let trimmedMessage = message.trimmingCharacters(in: NSCharacterSet.whitespaces) 
    let trimmedKey = key.trimmingCharacters(in: NSCharacterSet.whitespaces) 

    // Sets the key the same size as the message 
    let paddedTrimmedKey = trimmedKey.padding(toLength: message.characters.count, withPad: trimmedKey, startingAt: 0) 

    // separates the message and key into individual characters 
    let charTrimmedMessage = Array(trimmedMessage.characters) 
    let charPaddedTrimmedKey = Array(paddedTrimmedKey.characters) 


    // Compare the values in the key to the message and scrambles the message. 
    var i = 0 
    for chr in charTrimmedMessage{ 
     code.append((alphabet[String(chr)]! + alphabet[String(charPaddedTrimmedKey[i])]!) % 26) // <- I think the error comes from this line. Maybe the exclamation marks? 
     i += 1 
    } 


    var cyphr: String = "" // this will hold the return String 

    // I think I could have used some sort of "join" function here. 
    for number in code{ 
     cyphr = cyphr + alphabet2[number + 1]! 
    } 

    return cyphr 
} 

cipher(message: mess, key: key) // <--- this returns an error, no clue why. The code works and compiles great. 

는이 오류를 얻을 :

Error

날이 더 나은 같은 일을 방지하기 위해 내 코드를 개선하는 방법에 대한 포인터를 알려 수 있다면.

+0

향후 독자를 위해 오류의 이미지를 텍스트가 포함 된 코드 블록으로 바꾸십시오. 텍스트 이미지는 클립 보드, 검색 엔진 또는 스크린 리더와 호환되지 않습니다. – halfer

답변

2

alphabet 사전에는 일반 텍스트에 포함 된 "I", ""문자에 대한 조회 값이 없습니다.

(이 전무 인 선택을 풀기 힘이기 때문에) 암호 기능이 있기 때문에이 실패하면 mess 변수를 초기화하면 암호가 작동하도록 얻을 것이다

var mess = "ioncesawabigmosquito" 
cipher(...) -> "ypgvuttpqcbzcpljkjmh" 

어느

  • alphabet 조회 사전을 업데이트하십시오. 예 : '유효한 입력으로 역방향 조회를 제공하기 위해 alphabet2를 업데이트하고 (새 설명하기 위해 27 26에서 모드 요소를 변경'추가

또는

  • 사전에 귀하의 의견을 확인 '문자) . 단지 알파벳에서 유효한 문자를 포함하는 입력을 다듬

을 (유효 범위 내에 있지/스트립 문자를 소문자로 변환 공백을 대체, 대문자 공간 트림), 당신은 시도 할 수 :

let validSet = CharacterSet.init(charactersIn: alphabet.keys.joined()) 
var messTrimmed = mess.trimmingCharacters(in: validSet.inverted) 

:이 일을하는 것은


또 다른 버그가 원래 메시지에서 정보가 손실을 의미하는 것이

라인 cyphr = cyphr + alphabet2[number+1]!cyphr = cyphr + alphabet2[number]!이어야합니다.

코드 배열의 값이 mod 26으로 계산되고 알파벳의 최대 키 값이 25이기 때문에 숫자에 1을 더하는 것은 올바르지 않습니다. 존재하지 않는 키에 강제로 언 래핑 할 때 예외가 발생합니다.

예. 실패하게하려면 cipher(message: "ypgvuttpqcbzcpljkjmh", key: "pass")을 시도하십시오.


옆 완전한으로 각주

, 여기에 암호 기능의 변형이다 (I는 입력을 소독하지 않은가, 그냥 코드 기능 방식으로 작성 될 수있는 방법을 보여주는)

func cipher2(message: String, key: String) -> String { 
    return 
     message 
     .characters 
     .enumerated() 
     .map { ($0, String($1)) } 
     .map { 
      (
        alphabet[$1]! 
       + alphabet[String(key[key.index(key.startIndex, offsetBy: ($0 % key.characters.count))])]! 
      ) % 26 
     } 
     .map { alphabet2[$0]! } 
     .joined() 
} 
+0

Benzi, 정말 코드를 보았습니다. 어떻게 그 코드를 놓쳤는 지 확신하지 못했습니다. 그리고 당신의 코드를 연구 할게요. 도움에 감사드립니다. – lokilindo

관련 문제