최근에 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.
는이 오류를 얻을 :
날이 더 나은 같은 일을 방지하기 위해 내 코드를 개선하는 방법에 대한 포인터를 알려 수 있다면.
향후 독자를 위해 오류의 이미지를 텍스트가 포함 된 코드 블록으로 바꾸십시오. 텍스트 이미지는 클립 보드, 검색 엔진 또는 스크린 리더와 호환되지 않습니다. – halfer