【问题标题】:How to compare each letters in an array with all the letters in another array? - Swift如何将数组中的每个字母与另一个数组中的所有字母进行比较? - 斯威夫特
【发布时间】:2016-01-15 21:48:04
【问题描述】:

我有 2 个不同的数组,称为:criptedChar 和字母表。我需要检查 criptedChar 中的第一个字符(所以“criptedchar [0]”)并检查字母表中的对应关系。 举个例子: criptedChar // ["d","e","c","b"] 字母表 // ["a","b","c" 等等] 我想从 criptedChar[0] 中取出 d 并检查所有字母表中是否有“d”,然后将“d”的位置保存在第二个数组中。 我还需要增加 criptedChar 括号内的数字。我会从用户那里获取号码。 你能帮我么?谢谢!

func decript() {
    var criptedText = incriptedText.text!   //get text from uiTextField
    var criptedChar = Array<Character>(criptedText.characters)  //from text to char & all in array :D
    var alfabeto: Array<Character> = ["a","b", "c", "d", "e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    var capacityCriptedCharArray = criptedChar.capacity

    for (var i = 0; i < 26; i++) {
        if criptedChar[0] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }


    for (var i = 0; i < 26; i++) {
        if criptedChar[1] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }

    for (var i = 0; i < 26; i++) {
        if criptedChar[2] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }   
}

此代码有效,但它很笨,我无法控制用户输入

【问题讨论】:

    标签: arrays string swift swift2 character


    【解决方案1】:

    如果我正确理解您的问题,您正在寻找 像这样的东西(内联解释):

    // Start with your crypted text, and an empty string for the result:
    let cryptedText = "mifpyx"
    var decryptedText = ""
    
    // Two character arrays (of equal length):
    let alphabet   = Array("abcdefghijklmnopqrstuvwxyz".characters)
    let newLetters = Array("ghijklmnopqrstuvwxyzabcdef".characters)
    
    // For each character in the input string:
    for c in cryptedText.characters {
        // Check if `c` is contained in the `alphabet` array:
        if let index = alphabet.indexOf(c) {
            // Yes, it is, at position `index`!
            // Append corresponding character from second array to result:
            decryptedText.append(newLetters[index])
        }
    }
    
    print(decryptedText) // solved
    

    或者,您可以从 两个数组:

    var mapping = [ Character : Character ]()
    zip(alphabet, newLetters).forEach {
        mapping[$0] = $1
    }
    

    然后将输入中的每个字符映射到那个 字典:

    let decryptedText = Array(cryptedText.characters
            .map { mapping[$0] }
            .flatMap { $0 }
        )
    

    (这里flatMap用于从输入数组中不存在的字符中过滤掉nils。)

    【讨论】:

    • 非常感谢!我使用了你写的第一种方法:)
    【解决方案2】:

    以下是您可能可以适应您的应用程序的简单密码逻辑示例:

    let readableText = "the quick brown fox jumped over the lazy dog"
    
    // letters: letters in readable text that will be encoded
    // cypher : corresponding encoded letters
    //
    // note: letters and cypher must have the same number of elements
    let letters:[Character] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    let cypher:[Character]  = ["o","p","q","r","a","b","c","d","e","f","g","h","i","u","v","w","x","y","z","j","k","l","m","n","s","t"]
    
    // build a mapping disctionary from readable to encoded
    var encode:[Character:Character] = [:]
    for (index, letter) in letters.enumerate() { encode[letter] = cypher[index] } 
    
    // encrypt the readble text  gives: "jda xkeqg pyvmu bvn fkiwar vlay jda hots rvc"
    let cryptedText = String(readableText.characters.map({ encode[$0] ?? $0 })) 
    
    
    // build a mapping disctionary from encoded to readable
    var decode:[Character:Character] = [:]
    for (index, letter) in cypher.enumerate() { decode[letter] = letters[index] }
    
    // decrypted the encrypted text  gives: "the quick brown fox jumped over the lazy dog"
    let decryptedText = String(cryptedText.characters.map({ decode[$0] ?? $0 })) 
    

    【讨论】:

    • 谢谢!我没有在这个项目中使用它们,但它们可能对其他东西有用:))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    相关资源
    最近更新 更多